码迷,mamicode.com
首页 > 编程语言 > 详细

Python面向对象之继承

时间:2016-07-01 13:18:58      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:

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

 

Python面向对象之继承

标签:

原文地址:http://www.cnblogs.com/lqcdsns/p/5632577.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!