9.7.1.继承的基本概念:
- 类是用来描述现实世界中同一组事物的公有性的抽象模型
- 类也有上下级和范围之分,如:生物->动物->哺乳动物->灵长类动物->人类->黄种人
- 描述类共性和个性之间的关系
9.7.2.名词解释
- 继承:一个类从另一个已有的类获得其他成员的相关特性,称为继承
- 派生:从一个已有的类产生一个新的类称为派生
继承和派生其实是从不同角度和方向来描述相同的概念,本质上是一个东西
- 父类:他也称为基类,就是指已有被继承的类
- 子类:也称为了派生类或扩展类
- 扩展:在子类中增加一些自己特有的特性,称为扩展,没有扩展则继承无意义
- 单继承:一个类只能继承一个其他类,不能继承多个其他类,使用较多
- 多继承:一个类共同继承了多个父类
9.7.3.继承的基本语法:
- 假设A类要继承B类中的所有属性和方法(私有除外)
class B(object):
pass
class A(B): #A继承B
pass
a=A()
-
class Person(object): def eat(self): print('i an eat food!') def speak(self): print('i can speak!') class Teacher(Person): #继承Person pass class Student(Person): pass teacher1=Teacher() teacher1.eat() teacher1.speak() student1=Student() student1.eat() student1.speak()
9.7.4.单继承
- 单继承:一个类能继承自一个其他类,不能多继承
- 格式:
1.定义一个父类
class Person(object):
pass
2.定义一个子类
class Teacher(Person)
pass
- 类object:是所有类的父类,在该类中定义了所有类的公有默认方法,如:new()、init(),定义类时,只有类名没有继承相关参数,此时可以省略object
- 单继承:传递性,如继承B类,B类继承C类,根据传递性,A类也就会自动继承C类中所有公共属性和方法
9.7.5.多继承
- 概念:python少数几个支持多继承的编程语言。允许一个类继承多个类,如:
- 格式:
class B(object)
pass
class c(object)
pass
class A(B,C):
pass
- 例:
class GasolineCar(object): #汽油车
def run_with_gosoline(self):
print('i can with gasoline')
class EletricCar(object):
def run_with_eletricCar(self):
print('i can witj eletric')
class HybridCar(GasolineCar,EletricCar): #混动
pass
tesla=HybridCar()
tesla.run_with_gosoline()
tesla.run_with_eletricCar()
- 虽然语法允许使用多继承,到实际上应用尽量少用多继承,因为多个父类中的属性或方法的名称可能出现冲突
9.7.6.方法重新
- 扩展特性:继承让子类拥有父类的所有公共属性和方法,但如果仅仅是为了继承属性和方法就会失去继承的意义,应该子类拥有自己的属性和方法
- 重写:也叫覆盖,子类成员与父类成员相同,从父类继承的成员会重新定义,此时起效的为子类中定义的成员
- 注意:类方法的调用顺序:现在子类中查找,若找不到则在父类中查找
9.7.7.supper关键字
- supper():调用父类属性和方法
- 完整写法:
supper(当前的类名称,self).属性或方法()
- python3版本后
super().属性
#或者
super()方法名()
例:Car类
class Car(object):
def __init__(self, brand, model, color):
self.brand = brand
self.model = model
self.color = color
def run(self):
print('i can run')
class GasolineCar(Car): # 继承Car
def __init__(self, brand, model, color):
super().__init__(brand, model, color) # 调用父类Car的__init__()方法
def run(self):
print('i can with gasoline')
class EletricCar(Car):
def __init__(self, brand, model, color):
super().__init__(brand, model, color)
self.battery = 70
def run(self):
print(f'i can with eletric,remain:{self.battery}')
def run_with_eletricCar(self):
print('i can witj eletric')
bmw = GasolineCar('宝马', 'X5', '白色')
bmw.run()
tesla = EletricCar('特斯拉', 'Model S', '红色')
tesla.run()
- MRO属性或MRO方法
作用:获取类的层次结构
格式:
类名._ _mro_ _
#或
类名.mro()
例:上例查看结构
class Car(object):
def __init__(self, brand, model, color):
self.brand = brand
self.model = model
self.color = color
def run(self):
print('i can run')
class GasolineCar(Car): # 继承Car
def __init__(self, brand, model, color):
super().__init__(brand, model, color) # 调用父类Car的__init__()方法
def run(self):
print('i can with gasoline')
class EletricCar(Car):
def __init__(self, brand, model, color):
super().__init__(brand, model, color)
self.battery = 70
def run(self):
print(f'i can with eletric,remain:{self.battery}')
def run_with_eletricCar(self):
print('i can witj eletric')
print(EletricCar.__mro__) #查看层次结构,二选一
print(EletricCar.mro())