day06-2-Python面向对象特性:继承
-
面向对象编程 (OOP) 语言的一个主要功能就是“继承”。继承是指这样一种能力:它可以使用现有类的所有功能,并在无需重新编写原来的类的情况下对这些功能进行扩展。
通过继承创建的新类称为“子类”或“派生类”。
被继承的类称为“基类”、“父类”或“超类”。
继承的过程,就是从一般到特殊的过程。
要实现继承,可以通过“继承”(Inheritance)和“组合”(Composition)来实现。
在某些 OOP 语言中,一个子类可以继承多个基类。但是一般情况下,一个子类只能有一个基类,要实现多重继承,可以通过多级继承来实现。
继承概念的实现方式主要有2类:实现继承、接口继承。
实现继承是指使用基类的属性和方法而无需额外编码的能力;接口继承是指仅使用属性和方法的名称、但是子类必须提供实现的能力(子类重构爹类方法);在考虑使用继承时,有一点需要注意,那就是两个类之间的关系应该是“属于”关系。例如,Employee 是一个人,Manager 也是一个人,因此这两个类都可以继承 Person 类。但是 Leg 类却不能继承 Person 类,因为腿并不是一个人。 - 单继承
-
# class People: 经典类 class People(object): # 新式类 def __init__(self, name, age): self.name = name self.age = age def eat(self): print("%s is eating..." % self.name) def talk(self): print("%s is talking..." % self.name) def sleep(self): print("%s is sleeping..." % self.name) class Man(People): def __init__(self, name, age, money): # People.__init__(self,name,age)或者 # 经典类写法 super(Man, self).__init__(name, age) self.money = money print("重构对象添加新属性:%s" % self.money) def piao(self): print("%s is piaoing ..... 20s....done." % self.name) def sleep(self): People.sleep(self) print("man is sleeping ") # Yang is sleeping... # man is sleeping m1 = Man("Yang", 22, 10) m1.eat() m1.sleep() class Woman(People): def get_birth(self): print("%s is born a baby...." % self.name) w1 = Woman("Chen", 26) w1.get_birth() # 输出结果 # 重构对象添加新属性:10 # Yang is eating... # Yang is sleeping... # man is sleeping # Chen is born a baby....
-
多继承
-
class A: def __init__(self): print("A") class B(A): pass # def __init__(self): # print("B") class C(A): pass # def __init__(self): # print("C") class D(B,C):#只初始化B的__init__函数,不走C就停止 pass # def __init__(self): # print("D") obj = D() # 多继承:两中查找方式(广度优先,深度优先),默认广度优先 # Python2:经典类:深度优先,新式类广度优先 # python3:广度优先
案例:继承——学校
__author__ = "Alex Li" class School(object): def __init__(self,name,addr): self.name = name self.addr = addr self.students =[] self.staffs =[] def enroll(self,stu_obj): print("为学员%s 办理注册手续"%stu_obj.name ) self.students.append(stu_obj) def hire(self,staff_obj): self.staffs.append(staff_obj) print("雇佣新员工%s" % staff_obj.name) class SchoolMember(object): def __init__(self,name,age,sex): self.name = name self.age = age self.sex = sex def tell(self): print("tell:") class Teacher(SchoolMember): def __init__(self,name,age,sex,salary,course): super(Teacher,self).__init__(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) self.stu_id = stu_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("zlz",56,"MF",200000,"Linux") t2 = Teacher("zlf",22,"M",3000,"PythonDevOps") s1 = Student("张路风",36,"MF",1001,"PythonDevOps") s2 = Student("雷大侠",19,"M",1002,"Linux") t1.tell() s1.tell() school.hire(t1) school.enroll(s1) school.enroll(s2) print(school.students) print(school.staffs) school.staffs[0].teach() for stu in school.students: stu.pay_tuition(5000)
##########################################################################################################
#输出:
---- info of Teacher:zlz ----
Name:zlz
Age:56
Sex:MF
Salary:200000
Course:Linux
---- info of Student:张路风 ----
Name:张路风
Age:36
Sex:MF
Stu_id:1001
Grade:PythonDevOps
雇佣新员工zlz
为学员张路风 办理注册手续
为学员雷大侠 办理注册手续
[<__main__.Student object at 0x0000000002244668>, <__main__.Student object at 0x00000000022446A0>]
[<__main__.Teacher object at 0x00000000022445F8>]
zlz is teaching course [Linux]
张路风 has paid tution for $5000
雷大侠 has paid tution for $5000