クラス継承、ダイヤモンドの継承、導出、多型

まず、クラスの継承(再親に自分自身を発見する最初のオーダー)

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、新しいクラス(限り、デフォルトでは、継承されたように、オブジェクトクラス、オブジェクトを継承したのpython3のデフォルト)

2、クラシック(デフォルトなし継承されたオブジェクト、python2は古典的なカテゴリーです)

画像-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に示すように、深さ優先(クラシック)

画像-20190619115117989

4、幅優先(新しいスタイルのクラスは、ダイヤモンドの継承にのみ表示されます)

画像-20190619115217893

ファイブ多型、クラス

(動物の使用で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()

おすすめ

転載: www.cnblogs.com/chuwanliu/p/11056473.html