发布时间:2018-04-07 17:08:56编辑:admin阅读(4607)
面向对象编程语言具有封装、继承、多态三个基本特征,本文就继承举例详谈
比如说学校
主要有3大角色:
学校,讲师,学员
学校可以注册学员,统计有多少学员
老师负责讲课
学生听课,提问,学习,交学费
先定义4个类:
class School(object): #学校 def __init__(self): pass class SchoolMember(object): #学校成员 def __init__(self): pass class Teacher(SchoolMember): #讲师 def __init__(self): pass class Student(SchoolMember): #学生 def __init__(self): pass
再写几个类方法
class School(object): #学校 def __init__(self): pass def enroll(self): #注册 pass class SchoolMember(object): #学校成员 def __init__(self): pass def tell(self): #打印自己个人信息 pass class Teacher(SchoolMember): #讲师 def __init__(self): pass def tell(self): #重构父类方法,打印自己个人信息 pass def teach(self): #讲课 pass class Student(SchoolMember): #学生 def __init__(self): pass def tell(self): pass def pay_tuition(self,amount): #交学费 pass
完善类方法
class School(object): #学校 def __init__(self,name,addr): self.name = name #姓名 self.addr = addr #地址 self.students = [] #学生列表 self.teachers = [] #讲师列表 def enroll(self,stu_obj): #注册 print('为学员%s 办理注册手续'% stu_obj) self.students.append(stu_obj) class SchoolMember(object): #学校成员 def __init__(self,name,age,sex): self.name = name #姓名 self.age = age #年龄 self.sex = sex #性别 def tell(self): #打印自己个人信息 pass class Teacher(SchoolMember): #讲师 def __init__(self,name,age,sex,salary,course): super(Teacher,self).__init__(name,age,sex) #引用父类的name,age,sex self.salary = salary #工资 self.course = course #课程 def tell(self): #重构父类方法,打印自己个人信息 print(''' --- info of Teacher:%s --- Name:%s Age:%s Sex:%s Salary:%s Course:%s ''' % (self.name,self.name,self.age,self.sex,self.salary,self.course)) def teach(self): #讲课 print("%s is teaching course [%s]" % (self.name,self.course)) class Student(SchoolMember): #学生 def __init__(self,name,age,sex,stu_id,grade): super(Student,self).__init__(name,age,sex) #引用父类的name,age,sex self.stu_id = stu_id #学生id self.grade = grade #班级 def tell(self): print(''' --- info of Student:%s --- Name:%s Age:%s Sex:%s Stu_id:%s Grade:%s ''' % (self.name,self.name,self.age,self.sex,self.stu_id,self.grade)) def pay_tuition(self,amount): #交学费 print("%s has paid tution for $%s" % (self.name,amount))
实例化对象
school = School("老男孩IT","沙河") #实例化学校 t1 = Teacher("Oldboy",56,"M",2000000,"Linux") #实例化一个老师 t2 = Teacher("Alex",22,"M",3000,"PythonDevOps") s1 = Student("ChenRonghua",36,"M",1001,"PythonDevOps") #实例化一个学生 s2 = Student("Xuliangwei",19,"M",1002,"Linux")
执行类方法
t1.tell() #打印老师个人信息 s1.tell() #打印学生个人信息
执行输出:
--- info of Teacher:Oldboy ---
Name:Oldboy
Age:56
Sex:M
Salary:2000000
Course:Linux
--- info of Student:ChenRonghua ---
Name:ChenRonghua
Age:36
Sex:M
Stu_id:1001
Grade:PythonDevOps
雇佣一个老师,注册一个学生,完整代码如下:
#!/usr/bin/env python # coding: utf-8 __author__ = 'www.py3study.com' class School(object): #学校 def __init__(self,name,addr): self.name = name #姓名 self.addr = addr #地址 self.students = [] #学生列表 self.stalls = [] #员工列表 def enroll(self,stu_obj): #注册 print('为学员%s 办理注册手续'% stu_obj.name) self.students.append(stu_obj) def hire(self,stall_obj): #雇用老师 self.stalls.append(stall_obj) #添加员工 print('雇佣新员工 %s' % stall_obj.name) class SchoolMember(object): #学校成员 def __init__(self,name,age,sex): self.name = name #姓名 self.age = age #年龄 self.sex = sex #性别 def tell(self): #打印自己个人信息 pass class Teacher(SchoolMember): #讲师 def __init__(self,name,age,sex,salary,course): super(Teacher,self).__init__(name,age,sex) #引用父类的name,age,sex self.salary = salary #工资 self.course = course #课程 def tell(self): #重构父类方法,打印自己个人信息 print(''' --- info of Teacher:%s --- Name:%s Age:%s Sex:%s Salary:%s Course:%s ''' % (self.name,self.name,self.age,self.sex,self.salary,self.course)) def teach(self): #讲课 print("%s is teaching course [%s]" % (self.name,self.course)) class Student(SchoolMember): #学生 def __init__(self,name,age,sex,stu_id,grade): super(Student,self).__init__(name,age,sex) #引用父类的name,age,sex self.stu_id = stu_id #学生id self.grade = grade #班级 def tell(self): print(''' --- info of Student:%s --- Name:%s Age:%s Sex:%s Stu_id:%s Grade:%s ''' % (self.name,self.name,self.age,self.sex,self.stu_id,self.grade)) def pay_tuition(self,amount): #交学费 print("%s has paid tution for $%s" % (self.name,amount)) school = School("老男孩IT","沙河") #实例化学校 t1 = Teacher("Oldboy",56,"M",2000000,"Linux") #实例化一个老师 t2 = Teacher("Alex",22,"M",3000,"PythonDevOps") s1 = Student("ChenRonghua",36,"M",1001,"PythonDevOps") #实例化一个学生 s2 = Student("Xuliangwei",19,"M",1002,"Linux") t1.tell() #打印老师个人信息 s1.tell() #打印学生个人信息 school.hire(t1) #雇佣一个老师 school.enroll(s1) #注册一个学生
执行输出:
--- info of Teacher:Oldboy ---
Name:Oldboy
Age:56
Sex:M
Salary:2000000
Course:Linux
--- info of Student:ChenRonghua ---
Name:ChenRonghua
Age:36
Sex:M
Stu_id:1001
Grade:PythonDevOps
雇佣新员工 Oldboy
为学员ChenRonghua 办理注册手续
打印2个列表
print(school.students) #学生列表 print(school.stalls) #员工列表
执行输出:
[<__main__.Student object at 0x000001C7018BD390>]
[<__main__.Teacher object at 0x000001C7018BD320>]
这是2个对象
再注册一个学生,让第一个老师去讲课,最后一行添加
school.enroll(s2) #注册第二个学生 school.stalls[0].teach() #让第一个老师去讲课
执行输出:
Oldboy is teaching course [Linux]
让每个学生都交学费,最后一行添加
for stu in school.students: stu.pay_tuition(5000)
执行输出:
ChenRonghua has paid tution for $5000
Xuliangwei has paid tution for $5000
在这个例子中,SchoolMember和Teacher、Student是继承关系
Teacher、Student和School是组合关系,通过某种方式,组合在一起了。
他们之间产生了一些交互
这个例子不难,只要把逻辑理清就可以了。
上一篇: python 面向对象之继承顺序
下一篇: python 面向对象之多态
47604
45984
36909
34467
29079
25713
24565
19714
19245
17756
5564°
6155°
5690°
5737°
6704°
5482°
5484°
5988°
5965°
7295°