类的继承、菱形继承、派生、多态
一、类的继承(查找顺序先自己再父类)
class ParentFoo:
def __init__(self,first_name,car,money,house):
self.first_name=first_name
self.car=car
self.money=money
self.house=house
def teach(self):
print("%s教人"%self.first_name)
# f1=ParentFoo('zhu','tesla',10,'Haozhai')
class SonFoo(ParentFoo):
pass
f2=SonFoo('zhu','tesla',10,'Haozhai')
f2.teach()
#zhu教人
class Animal:
def __init__(self,name,height,weight):
self.height=height
self.weight=weight
self.name=name
def jiao(self):
print('%sjiao'%self.name)
class People(Animal):
# def __init__(self):
# pass
def read(self):
print('read')
f1=People('alex',110,100)
f1.jiao()
class Boo:
def f1(self):
print('我是f1')
print(self)
self.f2()
class Foo(Boo):
def f2(self):
print('woshi f2')
ff=Foo()
ff.f1()
#我是f1
<__main__.Foo object at 0x10d611d68>
woshi f2
二、类的派生(继承父类属性的情况下也使用自身属性)
#v1版本(通过父类调用自己的属性)
class Animal:
def __init__(self,height,weight):
self.height=height
self.weight=weight
def jiao(self):
print('%sjiao')
class People():
def __init__(self,name,age):
self.age=age
self.name=name
def read(self):
print('read')
f1=People('alex',19)
Animal.__init__(f1, 180, 100)
print(f1.__dict__)
#{'height': 180, 'age': 19, 'weight': 100, 'name': 'alex'}
#v2版本(在子类的__init__中调用父类,这种跟继承没关系)
class Animal:
def __init__(self,height,weight):
self.height=height
self.weight=weight
def jiao(self):
print('%sjiao')
class People(Animal):
def __init__(self,name,age):
Animal.__init__(self, 180, 100)
self.age=age
self.name=name
def read(self):
print('read')
f1=People('alex',19)
print(f1.__dict__)
#{'height': 180, 'weight': 100, 'age': 19, 'name': 'alex'}
#v3版本(*************************)
class Animal:
def __init__(self,height,weight):
self.height=height
self.weight=weight
def jiao(self):
print('%sjiao')
class People(Animal):
def __init__(self,name,age):
super().__init__(180, 100)
self.age=age
self.name=name
def read(self):
print('read')
f1=People('alex',19)
print(f1.__dict__)
#{'name': 'alex', 'weight': 100, 'height': 180, 'age': 19}
#v4版本(不在继承条件下报错)
class Animal:
def __init__(self,height,weight):
self.height=height
self.weight=weight
def jiao(self):
print('%sjiao')
class People():
def __init__(self,name,age):
super().__init__(180, 100)
self.age=age
self.name=name
def read(self):
print('read')
f1=People('alex',19)
print(f1.__dict__)
#报错
三、类派生应用
class People:
def __init__(self,name,age,gender):
self.name=name
self.age=age
self.gender=gender
def speak(self):
print('%s开始说话了'%self.name)
class Student(People):
def __init__(self,name,age,gender,school,course):
super().__init__(name,age,gender)
self.school=school
self.course=course
def choose_course(self):
print('%s选择了%s的%s课程'%(self.name,self.school,self.course))
class Teacher(People):
def __init__(self,name,age,gender,course):
super().__init__(name,age,gender)
self.course=course
def mark(self,student_name,score):
print("%s给%s学生打了%s分"%(self.name,student_name.name,score))
f1=Student('owen',18,'man','oldboy','python')
print(f1.__dict__)
f2=Teacher('alex',20,'woman','python')
print(f2.__dict__)
f2.mark(f1,20)
四、菱形继承
1、新式类(只要默认继承了object类,python3默认继承了object)
2、经典类(没有默认继承object,python2就是经典类)
![image-20190619114855582](/Users/haiyuanyang/Library/Application Support/typora-user-images/image-20190619114855582.png)
class G:
def test(self):
print('from G')
class E(G):
def test(self):
print('from E')
class F(G):
def test(self):
print('from F')
class E(G):
def test(self):
print('from E')
class B(E):
def test(self):
print('from B')
class C(F):
def test(self):
print('from C')
class D(G):
def test(self):
print('from D')
class A(B,C,D):
def test(self):
print('from A')
f=A()
f.test()
3、深度优先(经典类)
![image-20190619115117989](/Users/haiyuanyang/Library/Application Support/typora-user-images/image-20190619115117989.png)
4、广度优先(新式类,只出现在菱形继承中)
![image-20190619115217893](/Users/haiyuanyang/Library/Application Support/typora-user-images/image-20190619115217893.png)
五、类的多态
(只有拥有Animal的方法才能使用Animal内的类方法)
符合动物类规定的规则才是动物
import abc
class Animal(metaclass=abc.ABCMeta):
def __init__(self,height,weight):
self.height=height
self.weight=weight
@abc.abstractmethod
def speak(self):
print('开始叫了')
@abc.abstractmethod
def eat(self):
print('开始吃了')
def sleep(self):
print('开始睡觉',self)
class People(Animal):
def speak(self):
pass
def eat(self):
pass
f=People(100,200)
f.sleep()
#接口
def func(obj):
obj.eat()
func(Dog)
func(Zhu)