Class inheritance, diamond inheritance, derived, polymorphism

First, the class inheritance (order of the first to find themselves re-parent)

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

Second, the derived class (parent class inherits attributes case also uses its own attributes)

#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__)
#报错

Third, the application of the derived class

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)

Fourth, the diamond inheritance

1, the new class (as long as the default inherited object classes, python3 default inherited object)

2, Classic (no default inherited object, python2 is the classic category)

image-20190619114855582

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, depth-first (Classic)

image-20190619115117989

4, breadth-first (new-style class, appear only in the diamond inheritance)

image-20190619115217893

Five polymorphic, class

(The only way to have the Animal class method in Animal Use)

In line with the provisions of the rules of animal is an 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()

Guess you like

Origin www.cnblogs.com/chuwanliu/p/11056473.html