类的继承
代码如下:
class Person(object):
def __init__(self, name, age):
self._name = name
self._age = age
@property
def name(self):
return self._name
@property
def age(self):
return self._age
@age.setter
def age(self, age):
self._age = age
def play(self):
print('%s正在愉快的玩耍'% self._name)
def watch_av(self):
if self._age >= 18:
print('%s正在观看动作片' % self._name)
else:
print('%s正在观看熊出没' % self._name)
class Student(Person):
def __init__(self, name, age, grade, course):
super().__init__(name, age)
self._grade = grade
self._course = course
@property
def grade(self):
return self._grade
@grade.setter
def grade(self, grade):
self._grade = grade
def study(self):
print('%s正在学习%s' % (self._name, self._course))
def chengji(self):
print('%s的%s成绩为%d分' % (self._name, self._course, self._grade))
class Teacher(Person):
def __init__(self, name, age, title, course):
super().__init__(name, age)
self._title = title
self._course = course
@property
def title(self):
return self._title
@title.setter
def title(self, title):
self._title = title
def teach(self):
print('%s正在讲%s课' % (self._name, self._course))
def main():
man1 = Student('小明', 8, 90, '思想品德')
man2 = Teacher('张涛', 38, '教授', 'python课程设计')
man1.study()
man1.chengji()
man2.teach()
man1.play()
man1.watch_av()
man2.watch_av()
if __name__ == '__main__':
main()
运行结果: