Twenty

inherit

A succession

Cheng is one way to create a new class, the new class can inherit one or more parent classes (python support multiple inheritance), the parent class can be called a base class or super class, the new class is called the derived class or child class.

Subclass inherits the properties of the parent class, the code is too long to solve the redundancy

Inherited Category: new categories, Classic.

The new class: class inherits the object is new style classes, python 3, the default inherited object class, so are the new class. Only two inherited object is explicitly specified in the new class in python 2.

Classic: not inherit object class is the classic category, python 2 classic category, python 3 is not Classic

class A(object):
    pass
class C:
    pass
#B继承了A,C这个类
class B(A,C):
    pass

print(C.__bases__)

Second, the use of inheritance reduce code redundancy

In the development process of the process, if we define a class A, then you want to build another new class B, but most of the same class A and class B, we can not start from scratch to write a class B, which We use the concept of inheritance of classes.

By way of a new inheritance class B, so that B inherits A, B will 'genetic' all the attributes (data attribute and function attribute) of A, code reuse.

class Person(object):
    school = 'jialidun'
    
    def __init__(self,name,age):
        self.name = name
        self.age = age

class Teacher(Person):
    pass

class Student(Person):
    pass

# 类实例化会自动调用__init__如果类中没有,去父类中找
# stu1=Student()   #报错,因为父类中必须传两个参数
stu1 = Student('xian_hu',20)
print(stu1.name)
print(stu1.age)
print(stu1.school)
stu1.school='xxx'
print(stu1.school)
    
xiao_hu
18
jialidun
xxx

2.1 Multilayer inheritance

class A:
    a = 'AAAA'
class B(A):
    a = 'BBBB'
class C (B):
    a = 'CCCC'
class D(C):
    pass
d = D()
print(d.a)


CCCC

Inheritance is a multi-layer up to inherit

More than 2.2 inheritance

class A:
    a="AAAA"
    pass
class B:
    a="BBB"
    pass

class C:
    a="CCC"
    pass

class D(A,B,C):
    pass
d=D()
print(d.a)

AAAA
class A:
    # a="AAAA"
    pass
class B:
    a="BBB"
    pass

class C:
    a="CCC"
    pass

class D(A,B,C):
    pass
d=D()
print(d.a)

BBB

Multiple inheritance is inherited from left to right

2.3 Multilayer multiple inheritance (diamond problem)

class G(object):
    a = "GGG"
    pass
class F(G):
    pass
class E(G):
    pass
class D(G):
    pass
class C(F):
    pass
class B(E):
    pass
class A(B,C,D):
    pass

a=A()
print(a.a)

GGG

Diamond inheritance problem into new categories and Classic, two different search order.

img

img

The new class (py3 in all new style classes): breadth first --- Starting from the left, has been looking up to find the diamond in the apex end (not including the diamond-shaped vertex), continue to the next to inherit the parent class looking up, find diamond-shaped apex end (not including the diamond-shaped vertex), and finally found a diamond vertices.

Classic (only in py2 only): --- depth-first from the left, has been looking up to find the diamond in the apex end (including diamond vertex) continue to inherit under a parent looking up to find the diamond apex end (not including diamond point).

Third, the method of the parent class reuse

3.1 nomination road last name

Inherit the parent class method way: by name, but did not actually inheritance. The method used is a subclass of def + __init__ direct call parent class definition.

3.2 super keyword

Inherit the parent class method Second way: super keyword.

class Person(object):
    school = 'oldboy'
    def __init__(self,name,age):
        self.name=name
        self.age=age
    def study(self):
        print('study....')

class Student(Person):
    school = 'yyyy'
    def __init__(self,name,age,course):

        super().__init__(name,age)
       super(Student,self).__init__(name,age)
        self.course=course
    def study(self):
        # Person.study(self)
        super().study()
        # print("%s学生在学习"%self.name)

Note: super () will get a list of the parent object in accordance with mro; binding object to call the method, you need to pass the first parameter (self).

super keyword-based methods are new and Classic, python 3 using the newer class super (). Method. python 3 uses the super (class name, object). method.

Summary: There inheritance, they usually use super. If there is no inheritance or inherit more than one parent class, super accordance mro list to find, now want to name names using one of the methods of a parent class, we need to name names

Guess you like

Origin www.cnblogs.com/tangceng/p/11420113.html