Python Basics - Object-Oriented Composition, Inheritance

combination

Multiple data attributes are associated with an object instantiated by a class

# -*- coding:utf8 -*-
class Forest:                               #定义总类结合-森林
    def __init__(self,other):               #定义类的属性
        self.other = other                          #总类中的类别
        self.addr = Animal()                        #把其它类别结合到总类中
        self.type = Plant()
class Animal:                               #定义的动物类
    def _animal(self):                          #定义的动物属性
        print("这是动物类别")
class Plant:                                #定义的植物类
    def _plant(self):                       #定义的植物属性
        print("这是植物类别")
P = Forest("测试")                          #总类实例化
print(P.other)                              #总类自定义类输出
P.addr._animal()                            #总类结合的其它类输出
测试
这是动物类别

inherit

New methods can be derived (derived) through integration

single inheritance

# -*- coding:utf8 -*-
class Forest:                       #定义的父类
    def __init__(self,name):
        self.Name = name
    def action(self):
        print("%s是非常厉害的" %self.Name)
class Subclass(Forest):             #子类,(衍生)出新的类
    print("森林里边")
P = Subclass("猴子")                #实例化子类
P.action()                          #子类调用父类属性
森林里边
猴子是非常厉害的

multiple inheritance

# -*- coding:utf8 -*-
class Forest:                               #定义的父类1
    def __init__(self,name):
        self.Name = name
    def action1(self):
        print("%s是非常厉害的" %self.Name)
class Activity:                             #定义的父类2
    def __init__(self,obj):
        self.Obj = obj
    @staticmethod
    def action2(describe):
        print("非常的%s" %describe)
class Subclass(Forest,Activity):             #子类,(衍生)出新的类
    print("森林里边")

P = Subclass("猴子")                         #实例化子类
P.action1()                                  #调用父类1属性
P.action2("灵活")                            #调用父类2属性
森林里边
猴子是非常厉害的
非常的灵活

interface inheritance

The parent class specifies the class methods that the subclass must use, which is convenient for program normalization design

Interface inheritance example

# -*- coding:utf8 -*-
import abc
class All_file(metaclass=abc.ABCMeta):       #父类定义的方法,限定子类使用方法
    @abc.abstractmethod
    def a(self):                             #限制子类方法1
        pass
    @abc.abstractmethod                 
    def b(self):                             #限制子类方法2
        pass
class new(All_file):                          #子类继承的方法使用,必须要实现父类方法,否则无法实例化
    def a(self):
        print("继承的类方法1")
    def b(self):
        print("继承的类方法2")
P = new()
P.a()
P.b()
继承的类方法1
继承的类方法2

Inheritance order

characteristic

Python classes can inherit multiple classes, while Java and C# can only inherit one class

If a Python class inherits multiple classes, there are two ways to find it: depth first and breadth first

dissect

For each class defined, Python computes a method resolution order (MRO) list, which is a linearly ordered list of all base classes. The construction of the MRO list is achieved by a C3 linearization algorithm.

MRO Listing Guidelines

Subclasses are checked before superclasses

Multiple parent classes are checked according to their order in the list

If there are two valid choices for the next class, choose the first parent class

Example

Inheritance order result example

# -*- coding:utf8 -*-
class A:
    def test(self):
        print("this is A")
class B(A):
    def test(self):
        print("this is B")
class C(A):
    def test(self):
        print("this is C")
class D(B,C):
    def test(self):
        print("this is E")

print(D.__mro__)
(<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324424918&siteId=291194637