Inheritance 继承
面向对象编程 (OOP) 语言的一个主要功能就是“继承”。继承是指这样一种能力:它可以使用现有类的所有功能,并在无需重新编写原来的类的情况下对这些功能进行扩展。
通过继承创建的新类称为“子类”或“派生类”。
被继承的类称为“基类”、“父类”或“超类”。
继承的过程,就是从一般到特殊的过程。
要实现继承,可以通过“继承”(Inheritance)和“组合”(Composition)来实现。
在某些 OOP 语言中,一个子类可以继承多个基类。但是一般情况下,一个子类只能有一个基类,要实现多重继承,可以通过多级继承来实现。
继承概念的实现方式主要有2类:实现继承、接口继承。
Ø 实现继承是指使用基类的属性和方法而无需额外编码的能力;
Ø 接口继承是指仅使用属性和方法的名称、但是子类必须提供实现的能力(子类重构爹类方法);
在考虑使用继承时,有一点需要注意,那就是两个类之间的关系应该是“属于”关系。例如,Employee 是一个人,Manager 也是一个人,因此这两个类都可以继承 Person 类。但是 Leg 类却不能继承 Person 类,因为腿并不是一个人。
抽象类仅定义将由子类创建的一般属性和方法。
OOP开发范式大致为:划分对象→抽象类→将类组织成为层次化结构(继承和合成) →用类与实例进行设计和实现几个阶段。
一个类可以派生出子类,在这个父类里定义的属性、方法自动被子类继承。继承简单举例:
#Author:Zheng Na class Person: 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(Person): # 子类可以继承父类的方法 # 子类可以定义自己的方法 def smoke(self): print("%s is smoking..." % self.name) # 子类可以重构父类的方法 def sleep(self): Person.sleep(self) print("man is sleeping...") class Woman(Person): def get_birth(self): print("%s is born a baby..." % self.name) m1 = Man("Alex",21) m1.eat() # Alex is eating... m1.smoke() # Alex is smoking... m1.sleep() # Alex is sleeping... man is sleeping... w1 = Woman("Cendy",23) w1.get_birth() # Cendy is born a baby... #w1.smoke() # Woman不能调用Man里面的方法,会报错:AttributeError: 'Woman' object has no attribute 'smoke'
对构造函数进行重构:
#Author:Zheng Na class Person: 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(Person): # 对构造函数进行重构:给Man多传一个参数 # 注意:父类的参数也要都写上 def __init__(self,name,age,money): #以下2种写法都是对的,但使用super的好处有2个, # 一个是当父类Person名字变了的时候,子类Man只需要在继承的地方修改一次即可,不需要每个重构的方法都改一次; # 另一个是当子类同时继承多个父类时,重构时 写法2 只需要写一遍即可,但是 写法1 需要写多遍(每个父类写一遍),使用super更省代码 # Person.__init__(self,name,age) # 写法1 super(Man,self).__init__(name,age) # 写法2 self.money = money m1 = Man("Alex",21,100) print(m1.money) # 100
对于多继承,有的语言支持,有的语言不支持。比如说Python是支持多继承的,而java就是不支持多继承的。多继承举例:
#Author:Zheng Na # class Person: #经典类 class Person(object): #新式类 def __init__(self,name,age): self.name = name self.age = age self.friends = [] 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 Relation(object): def makefriends(self,obj): print("%s is making friends with %s" % (self.name,obj.name)) self.friends.append(obj) # 注意这里一定是obj而不是name #多继承 class Man(Person,Relation):#两个父类的执行顺序是从左到右 pass class Woman(Person,Relation): pass m1 = Man("Alex",21) w1 = Woman("Cendy",23) m1.makefriends(w1) # Alex is making friends with Cendy print(m1.friends) # [<__main__.Woman object at 0x00000000027C8BE0>] print(m1.friends[0].name) # Cendy w1.name = "Amy" #即使w1改名字了,m1交的朋友也还是w1 print(m1.friends[0].name) # Amy
通过“继承”(Inheritance)和“组合”(Composition)来实现继承举例:
#Author:Zheng Na #object是所有类的基类 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.name) self.students.append(stu_obj) def hire(self,tch_obj): print("雇佣新教师 %s " %tch_obj.name) self.teachers.append(tch_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) 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 %s course" % (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 tuition for $%s" % (self.name,amount)) school = School("长安大学","西安") t1 = Teacher("Steven",43,"M",10000,"Linux") t2 = Teacher("Martin",28,"M",8000,"Python") s1 = Student("Amy",20,"F",1001,2) s2 = Student("Bob",20,"M",1002,2) t1.tell() s1.tell() school.enroll(s1) school.enroll(s2) school.hire(t1) print(school.students) print(school.teachers) school.teachers[0].teach() for stu in school.students: stu.pay_tuition(6000)