举例:学校把学生分为Junior和Senior管理,学校统一录入他们的姓名,年龄,都安排他们上课,实践,放假。上课内容:Junior学生主要接受通识教育,Senior学生上专业课。教师可以同时给这两类学生上课。对Junior学生成绩打分,对Senior学生论文写评语。
Junior和Senior继承Student类:
Student属性有:name,age
Juniort除继承属性外还有:score
Senior除继承属性外还有:essay
那各自的构造方法怎么实现呢:
class Student(object):
def __init__(self,name,age):
self.__name=name
self.__age=age
class Junior(Student):
def __init__(self,name,age,sore):
super(Junior,self).__init__(name,age)
self.sore=sore
class Senior(Student):
def __init__(self,name,age,essay):
super(Senior,self).__init__(name,age)
self.essay=essay
要点:
对于Student的属性name, age,不希望外部随意修改。所以要是用__*attr1这种方式来声明为私有属性,相应的,基类和子类要对应写getter和setter方法。
基类的getter和setter:
def set_age(self,age):
if 16<age<25:
self.__age=age
else:
print('invalid age')
def get_age(self):
return self.__age
子类的getter和setter,要对Senior的年龄和Junior的年龄分段。
def set_age(self,age):
if 16<age<21:
super(Junior,self).set_age(age)
else:
print('invalid age')
def get_age(self):
super(Junior,self).get_age()
要点:
教师要给学生上课,意味着可以给Junior学生上课,也可以给Senior学生上课。遵循开闭原则:
扩展开放:Junior类和Senior类通过继承,将Student类扩展。
修改开放:不需要修改学生上课的方法,通过子类的重写,实现基类方法在子类的不同动作。
定义Student类的方法:
def lecture(self):
print('Having classes')
Senior类重写:
def lecture(self):
print('Liberal Education')
return self.sore
Junior类重写:
def lecture(self):
print('Professional Courses')
return self.essay
创建教师类,初始化,定义上课方法:
class Teacher():
def __init__(self,name):
self.name=name
def haveclass(self,student):
print('give knowledge')
student.lecture()
实例化所有对象,完成真正的调用:
selina_student=Junior('Selina',18,80)
adam_student=Senior('Adam',24,'title:******')
amy_teacher=Teacher('Amy')
amy_teacher.haveclass(selina_student)
john_teacher=Teacher('john')
john_teacher.haveclass(adam_student)
要点:
完整代码:
import sys
from functools import reduce
class Student(object):
def __init__(self,name,age):
self.__name=name
self.__age=age
def set_name(self,name):
if not isinstance(name,str):
print('invalid name')
self.__name = name
def get_name(self):
return self.__name
def set_age(self,age):
if 16<age<25:
self.__age=age
else:
print('invalid age')
def get_age(self):
return self.__age
def lecture(self):
print('Having classes')
def fieldwork(self):
print('Do fieldwork monthly')
def holidy(self):
print('No lecture!')
class Junior(Student):
def __init__(self,name,age,sore):
super(Junior,self).__init__(name,age)
self.sore=sore
def set_name(self,name):
super(Junior,self).set_name(name)
def get_name(self):
super(Junior,self).get_name()
def set_age(self,age):
super(Junior,self).set_age(age)
def get_age(self):
super(Junior,self).get_age()
def lecture(self):
print('Liberal Education')
return self.sore
def fieldwork(self):
print('Talk with staffs')
def holidy(self):
print('Play with friends')
class Senior(Student):
def __init__(self,name,age,essay):
super(Senior,self).__init__(name,age)
self.essay=essay
def set_name(self,name):
super(Senior,self).set_name(name)
def get_name(self):
super(Senior,self).get_name()
def set_age(self,age):
super(Senior,self).set_age(age)
def get_age(self):
super(Senior,self).get_age()
def lecture(self):
print('Professional Courses')
return self.essay
def fieldwork(self):
print('Conduct Experiments')
def holidy(self):
print('Travel around')
class Teacher():
def __init__(self,name):
self.name=name
def haveclass(self,student):
print('give knowledge')
student.lecture()
def evaluation_junior(self,junior):
if junior.sore>=80:
junior.get_name()
print(' well done!')
elif 70<junior.sore<80:
junior.get_name()
print(' make more effort')
else:
junior.get_name()
print(' You have faild!')
def evaluation_senior(self,senior):
print("scan the essay")
print(senior.essay+' '+'evaluation')
selina_student=Junior('Selina',18,80)
adam_student=Senior('Adam',24,'title:******')
amy_teacher=Teacher('Amy')
print('Teacher:Amy '+'Junior Student:Selina')
amy_teacher.haveclass(selina_student)
amy_teacher.evaluation_junior(selina_student)
print('\n')
john_teacher=Teacher('john')
print('Teacher:John '+'Junior Student:Adam')
john_teacher.haveclass(adam_student)
john_teacher.evaluation_senior(adam_student)