Python Inheritance
Inheritance का उपयोग कर parent class की सभी properties(data member व member function) को child class में उपयोग किया जाता है।
Parent class जो inherit की जाती है उस class को कहते है और child class वो class है जो दूसरे क्लास को inherit करती है।
Example
class ParentClass: # parent class name
price = 100
def __init__(self):
print "Hello parent constructor"
def myParentMethod(self):
print 'parent method'
def setAmount(self, amt):
Parent.price = amt
def getAmount(self):
print "price is :", Parent.price
class ChildClass(Parent): # child class name
def __init__(self):
print "Hello child constructor"
def myChildMethod(self):
print 'Inside child method'
c = ChildClass() # Create object of child
c.myChildMethod() # calls child method
c.myParentMethod() # calls parent's method
c.setAmount(1200) # again parent's method call to set amount
c.getAmount() # again parent's method call to get amount
0 टिप्पणियाँ
If you have any doubt. Please let me know