继承是一种创建新类的方式,在python中,新建的类可以继承一个或多个父类,父类又可称为基类或超类,新建的类称为派生类或子类
class Animal: def __init__(self,name,sex,age): self.name = name self.sex = sex self.age = age class Person: def __init__(self,name,sex,age): self.name = name self.sex = sex self.age = age class Cat: def __init__(self,name,sex,age): self.name = name self.sex = sex self.age = age class Dog: def __init__(self,name,sex,age): self.name = name self.sex = sex self.age = age
我们可以看到多个类拥有相同的方法,怎样可以更方便些呢?
class Animal: def __init__(self,name,sex,age): self.name = name self.sex = sex self.age = age class Person(Animal): pass class Cat(Animal): pass class Dog(Animal): pass
初识继承:
子类以及子类实例化的对象 可以访问父类的任何方法或变量.
类名可以访问父类所有内容
子类实例化的对象也可以访问父类所有内容
class Animal: mind="有思想的" def __init__(self,name,sex): self.name=name self.sex=sex def eat(self): print("都会吃") class Dog(Animal): pass class Cat(Animal): pass class Chicken(Animal): pass a=Dog("xiaohu","nan") print(a.__dict__) 结果:{'name': 'xiaohu', 'sex': 'nan'} print(Dog.mind) 结果:有思想的 print(Dog.eat(1)) 结果:都会吃 print(a) 结果:<__main__.Dog object at 0x0000022F63921320> a.eat() 结果:都会吃 print(a.mind) 结果:有思想的
查询顺序
只执行父类的方法:子类中不要定义与父类同名的方法
只执行子类的方法:在子类创建这个方法.
既要执行子类的方法,又要执行父类的方法?
方法一:
class Animal: def __init__(self,name,sex,age): self.name=name self.sex=sex self.age=age def eat(self): print("%s吃东西" % self.name) def daink(self): print("%s喝东西" % self.name) class Bird(Animal): def __init__(self,name,sex,age,wing): Animal.__init__(self,name,sex,age) self.wing=wing def eat(self): print("吃东西") class Chook(Animal): pass {'name': '鹦鹉', 'sex': '雌性', 'age': 18, 'wing': '彩色'}
方法二:
class Animal: def __init__(self,name,sex,age): self.name=name self.sex=sex self.age=age def eat(self): print("%s吃东西" % self.name) def daink(self): print("%s喝东西" % self.name) class Bird(Animal): def __init__(self,name,sex,age,wing): super(Bird, self).__init__(name,sex,age) self.wing=wing def eat(self): print("吃东西") class Chook(Animal): pass b1=Bird("鹦鹉","雌性",18,"彩色") print(b1.__dict__) {'name': '鹦鹉', 'sex': '雌性', 'age': 18, 'wing': '彩色'}
class Animal: def __init__(self,name,sex,age): self.name=name self.sex=sex self.age=age def eat(self): print("%s吃东西" % self.name) def daink(self): print("%s喝东西" % self.name) class Bird(Animal): def __init__(self,name,sex,age,wing): # Animal.__init__(self,name,sex,age) super(Bird, self).__init__(name,sex,age) self.wing=wing def eat(self): super(Bird, self).eat() print("鸟吃虫子..") b1=Bird("鹦鹉","雌性",18,"彩色") b1.eat() 鹦鹉吃东西 鸟吃虫子..
class Animal: def __init__(self,name,sex,age): self.name=name self.sex=sex self.age=age def eat(self,a): print("%s吃%s" % (self.name,a)) def daink(self): print("%s喝东西" % self.name) class Bird(Animal): def __init__(self,name,sex,age,wing): # Animal.__init__(self,name,sex,age) super(Bird, self).__init__(name,sex,age) self.wing=wing def eat(self,b): super().eat(b) print("鸟吃虫子..") b1=Bird("鹦鹉","雌性",18,"彩色") b1.eat("方便面") 鹦鹉吃方便面 鸟吃虫子..
继承的进阶
继承: 单继承,多继承
类: 经典类, 新式类
新式类: 凡是继承object类都是新式类.
python3x 所有的类都是新式类,因为python3x中的类都默认继承object.
class A: pass
经典类: 不继承object类都是经典类
python2x:(既有新式类,又有经典类) 所有的类默认都不继承object类,所有的类默认都是经典类.你可以让其继承object.
class A(object): pass
单继承: 新式类,经典类查询顺序一样.
class A: def func(self): print('IN A') class B(A): def func(self): print('IN B') class C(B): def func(self): print('IN C') c1 = C() c1.func()
多继承:
新式类: 遵循广度优先.
经典类: 遵循深度优先.
多继承的新式类 广度优先 : 一条路走到倒数第二级,判断,如果其他路能走到终点,则返回走另一条路.如果不能,则走到终点.
print(F.mro()) 查询类的继承顺序
class A: def func(self): print('IN A') class B(A): def func(self): print('IN B') class C(A): def func(self): print('IN C') class D(B): def func(self): print('IN D') class E(C): def func(self): print('IN E') class F(D,E): def func(self): print('IN F') print(F.mro()) [<class '__main__.F'>, <class '__main__.D'>, <class '__main__.B'>, <class '__main__.E'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>]
多继承的经典类 深度优先 : 一条路走到底.(Python2中执行)
class A: #<=>转化成 新式类 class A(object) def func(self): print('IN A') class B(A): def func(self): print('IN B') class C(A): def func(self): print('IN C') class D(B): def func(self): print('IN D') class E(C): def func(self): print('IN E') class F(D,E): def func(self): print('IN F') f1 = F() f1.func()