Python class inherit example

class Staff:  
    def __init__(self,name,age):  
        self.name = name  
        self.age = age  
        print 'Create Staff: ', self.name  
  
    def tell(self):  
        print 'name:%s; age:%s' % (self.name, self.age)  
        
    def speak(self):
        print 'I\'m %s'%self.age
  
class Teacher(Staff):  
    def __init__(self,name,age,salary):  
        Staff.__init__(self,name,age)  
        self.salary = salary  
        print 'Create Teacher: ', self.name  
  
    def tell(self):  
        Staff.tell(self)  
        print 'salary: ', self.salary  
  
class Student(Staff):  
    def __init__(self,name,age,marks):  
        Staff.__init__(self,name,age)  
        self.marks = marks  
        print 'Create Student: ', self.name  
  
    def tell(self):  
        Staff.tell(self)  
        print 'marks: ', self.marks  
  
tea = Teacher('Eva', 28, 3000)  
stu = Student('Adam', 16, 77)  

have= [tea,stu]  

print 
  
for i in have:  
    print i.tell()
    print 
    print i.speak()
    print 


Create Staff:  Eva
Create Teacher:  Eva
Create Staff:  Adam
Create Student:  Adam

name:Eva; age:28
salary:  3000
None

I'm 28
None

name:Adam; age:16
marks:  77
None

I'm 16
None

Leave a Reply

Your email address will not be published.

Notice: Undefined index: cookies in /var/www/html/wp-content/plugins/live-composer-page-builder/modules/tp-comments-form/module.php on line 1638