Case of inheritance polymorphism in class in Python

  Polymorphism: uniformly call the same method in each class, so that each object has the same interface

class SchoolPerson:
     "" " school who " "" 

    DEF  __init__ (Self, name, Age):   # create an instance method 
        self.name, self.age = name, Age
         Print (f " members of the school instantiated: {self. name} " ) 

    def say (self):
         print (f " Name: {self.name} \ nAge: {self.age} " ) 


class Teacher (SchoolPerson):
     " "" 
    Teacher, inherit the SchoolPerson class 
    "" " 

    def  __init__ (self, name, age, salary):
         "" " 

        : param name:
        : param age: age
        : param salary: salary
        "" " 
        # Use super () + __init__ call parent class init method, automatic transmission Self 
        # used without parentheses may be super 
        super (). The __init__ (name, Age) 
        self.salary = the salary 

    DEF say (Self):
         # Class name + parent's say (self) 
        SchoolPerson.say (self)
         print (f " Salary: {self.salary} " ) 


class Student (SchoolPerson):
     "" " 
    Student, inherit SchoolPerson class 
    " "" 

    def  __init__ (self , name, Age, Score):
         # parent class name + init method, specifying Self 
        SchoolPerson.__init__(self, name, age) 
        self.score = score 

    def say (self): 
        SchoolPerson.say (self) 
        print (f " score: {self.score} " ) 


t = Teacher ( " 可 优" , 17, " confidential " ) 
s1 = Student ( " 小 优优" , 22, " 90 " ) 
s2 = Student ( " 小 明" , 16, " 99.99 " ) 

persons = [t, s1,
s2]
for per in persons:
    per.say()

 

 

******* Please respect the original, if you want to reprint, please indicate the source: Reprinted from: https://www.cnblogs.com/shouhu/ , thank you! ! ******* 

Guess you like

Origin www.cnblogs.com/shouhu/p/12743759.html