1.继承引入,减少代码量
1)版本1:
class Animal: '''定义一个动物类''' def eat(self): print("----吃----") def drink(self): print("----喝----") def sleep(self): print("----睡觉----") def run(self): print("----跑----")class Dog: def eat(self): print("----吃----") def drink(self): print("----喝----") def sleep(self): print("----睡觉----") def run(self): print("----跑----") def bark(self): print("-----汪汪叫---")a = Animal()a.eat()wangcai = Dog()wangcai.eat()
----吃--------吃----
2)版本2:继承动物类
class Animal: '''定义一个动物类''' def eat(self): print("----吃----") def drink(self): print("----喝----") def sleep(self): print("----睡觉----") def run(self): print("----跑----")class Dog(Animal): ''' def eat(self): print("----吃----") def drink(self): print("----喝----") def sleep(self): print("----睡觉----") def run(self): print("----跑----") ''' def bark(self): print("-----汪汪叫---")a = Animal()a.eat()wangcai = Dog()wangcai.eat()
3)版本3:
class Animal: '''定义一个动物类''' def eat(self): print("----吃----") def drink(self): print("----喝----") def sleep(self): print("----睡觉----") def run(self): print("----跑----")class Dog(Animal): def bark(self): print("-----汪汪叫---")class Cat(Animal): def catch(self): print("---抓老鼠---")a = Animal()a.eat()wangcai = Dog()wangcai.eat()tom = Cat()tom.eat()
2.子类继承父类的父类
class Animal: '''定义一个动物类''' def eat(self): print("----吃----") def drink(self): print("----喝----") def sleep(self): print("----睡觉----") def run(self): print("----跑----")class Dog(Animal): def bark(self): print("-----汪汪叫---")class Xiaotq(Dog): def fly(self): print("---飞----")xiaotianquan = Xiaotq()xiaotianquan.fly()xiaotianquan.bark()xiaotianquan.eat()
---飞---------汪汪叫-------吃----
3.重写
先在自己的类中查找父类的同名方法,没有的话去父类查找
class Animal: '''定义一个动物类''' def eat(self): print("----吃----") def drink(self): print("----喝----") def sleep(self): print("----睡觉----") def run(self): print("----跑----")class Dog(Animal): def bark(self): print("-----汪汪叫---")class Xiaotq(Dog): def fly(self): print("---飞----") def bark(self): #定义和父类方法同名的方法,就是重写 print("---重写叫---")xiaotianquan = Xiaotq()xiaotianquan.fly()xiaotianquan.bark()xiaotianquan.eat()
---飞-------重写叫-------吃----
4.调用被重写的方法
既要调用父类的方法,又要重写该方法
1)版本1: Dog.bark(self)
class Animal: '''定义一个动物类''' def eat(self): print("----吃----") def drink(self): print("----喝----") def sleep(self): print("----睡觉----") def run(self): print("----跑----")class Dog(Animal): def bark(self): print("-----汪汪叫---")class Xiaotq(Dog): def fly(self): print("---飞----") def bark(self): print("---重写叫---") #第一种调用被重写的父类的方法 Dog.bark(self)xiaotianquan = Xiaotq()xiaotianquan.fly()xiaotianquan.bark()xiaotianquan.eat()
---飞-------重写叫--------汪汪叫-------吃----
2)版本2:super().dark()
class Animal: '''定义一个动物类''' def eat(self): print("----吃----") def drink(self): print("----喝----") def sleep(self): print("----睡觉----") def run(self): print("----跑----")class Dog(Animal): def bark(self): print("-----汪汪叫---")class Xiaotq(Dog): def fly(self): print("---飞----") def bark(self): print("---重写叫---") #第一种调用被重写的父类的方法 # Dog.bark(self) #第2种 super().bark() #super()高级的,上级的xiaotianquan = Xiaotq()xiaotianquan.fly()xiaotianquan.bark()xiaotianquan.eat()
5.私有属性私有方法,不能被直接继承
私有属性私有方法可以被公有方法调用,公有方法可以被继承
1)版本1:
class A: def __init__(self): self.num1 = 100 self.__num2 = 222 def test1(self): print("---test1---") def __test2(self): print("--test2---") class B(A): pass b = B() b.test1() b.__test2() #私有方法不会被继承 print(b.num1) print(b.__num2) #私有属性不会被继承
---test1---Traceback (most recent call last): File "09-私有在继承中的表现.py", line 17, inb.__test2() #私有方法不会被继承AttributeError: 'B' object has no attribute '__test2'
2)版本2:
class A: def __init__(self): self.num1 = 100 self.__num2 = 222 def test1(self): print("---test1---") def __test2(self): print("--test2---")class B(A): passb = B()b.test1()#b.__test2() #私有方法不会被继承print(b.num1)#print(b.__num2) #私有属性不会被继承
---test1---100
3)版本3:通过公有方法调用
class A: def __init__(self): self.num1 = 100 self.__num2 = 222 def test1(self): print("---test1---") def __test2(self): print("--test2---") def test3(self): self.__test2() print(self.__num2)class B(A): passb = B()b.test1()#b.__test2() #私有方法不会被继承print(b.num1)#print(b.__num2) #私有属性不会被继承b.test3() #公有方法调用私有属性方法
---test1---100--test2---222
4)版本4:
class A: def __init__(self): self.num1 = 100 self.__num2 = 222 def test1(self): print("---test1---") def __test2(self): print("--test2---") def test3(self): self.__test2() print(self.__num2)class B(A): def test4(self): self.__test2() print(self.__num2)b = B()b.test1()#b.__test2() #私有方法不会被继承print(b.num1)#print(b.__num2) #私有属性不会被继承b.test3()b.test4()
---test1---100--test2---222Traceback (most recent call last): File "09-私有在继承中的表现.py", line 29, inb.test4() File "09-私有在继承中的表现.py", line 18, in test4 self.__test2()AttributeError: 'B' object has no attribute '_B__test2'
6.多继承
经典类
class Dog: pass
新式类 object是所有类的父类
class Dog(object): pass
多继承
class Base(object): def base(self): print("----this is base-")class A(Base): def test1(self): print("---this is A---")class B(Base): def test2(self): print("----this is B---")class C(A,B): passc1 = C()c1.test1()c1.test2()c1.base()
---this is A-------this is B-------this is base-
7.多继承注意点
切记:不要出现相同的方法名
1)版本1:先调用自身的
class Base(object): def test(self): print("----this is base-")class A(Base): def test(self): print("---this is A---")class B(Base): def test(self): print("----this is B---")class C(A,B): def test(self): print("----this is C---") c1 = C()c1.test()
----this is C---
2)版本2:调用哪个父类的AorB??
class Base(object): def test(self): print("----this is base-") class A(Base): def test(self): print("---this is A---") class B(Base): def test(self): print("----this is B---") class C(A,B): pass # def test(self): # print("----this is C---") c1 = C() c1.test()
---this is A---
3)版本3:调用顺序
class Base(object): def test(self): print("----this is base-") class A(Base): def test(self): print("---this is A---") class B(Base): def test(self): print("----this is B---") class C(A,B): pass # def test(self): # print("----this is C---") c1 = C() c1.test() print(C.__mro__)