python不支持多态
代码区:
1 class UniversityMember: 2 def __init__(self, name, age): 3 self.name = name; 4 self.age = age; 5 6 # 实例方法 7 def getName(self): 8 return self.name; 9 10 def getAge(self): 11 return self.age; 12 13 # 子类学生类继承父类 14 class Student(UniversityMember): 15 def __init__(self, name, age, height): 16 # 显示调用父类构造方法 17 UniversityMember.__init__(self, name, age); 18 self.height = height; 19 20 def getHeight(self): 21 return self.height; 22 23 # 子类老师类继承父类 24 class Teacher(UniversityMember): 25 def __init__(self, name, age, salary): 26 # 显示调用父类的构造方法 27 UniversityMember.__init__(self, name, age); 28 self.salary = salary; 29 30 def getSalary(self): 31 return self.salary; 32 33 # 实例化学生类 34 student = Student(‘stu1‘, 100, 158); 35 print(student.getName()); # stu1 36 print(student.getAge()); # 100 37 print(student.getHeight()); # 158 38 39 # 实例化老师类 40 teacher = Teacher(‘tea1‘, 100, 99); 41 print(teacher.getName()); # tea1 42 print(teacher.getAge()); # 100 43 print(teacher.getSalary()); # 99
时间: 2024-10-10 12:22:16