day21_7.25 object-oriented inheritance of

One. inherit

  What is inherited?

  Inheritance is a relationship between the two is what is described what relationship.

  In the program, inheritance describes the relation between class and class.

  For example, if a inherit b, a will have all the variables and methods b can be called directly.

class B:
    text=2

class A(B):
    pass

print(A.text)
#2

  called a subclass, b is called the parent class, which is the base class.

  Why should you use inheritance?

  In the process, the use of inheritance will put a lot of efficiency.

  It inherited the party directly inherited one party has some things. This allows the reuse already existing code to improve reusability.

  How to use inheritance?

  In the above example we have used once inherited.

  In python, a subclass can inherit more than one parent class, it can not be done in other languages.

two. abstract

  Abstract is more important for a concept object.

  Abstract definition is not specific, not cleared, to see things not understand.

  In many classes, found common ground, to re-establish a common class, this process is called abstraction. After the abstract, all classes have their common point can this abstract class as their parent.

  All in order to simplify the development process, you should:

  1. The first abstract and then inherit

  2. inherit an existing class, extend or modify the original function.

three. Property search order

class A1:
    text=1

class B2(A1):
    text=2

b1=B2()
b1.text=3
print(b1.text)

  When the output b1.text its own no text object is assigned the class will look for a default value, if, none of the class, looking at the parent class. Find all order are:

  The object itself -> where the class -> find parent -> parent class parent -> Object

four. Derivative

  When there is a parent class with different contents of a word class, word class is a derived class.

  When a parent class and subclass exactly the same, this subclass would be no longer necessary, therefore, in python each class is a derived class. Derived class refers to a subclass.

Fives. Covering overrides

  Coverage refers to the subclass appears in the properties and methods of the parent class name exactly the same answer.

six. Subclass access the parent class

  In a subclass, if some way to access the parent class you want, need to use super (), method:

  1.super property or method (current class name, self). You have to adjust the parent class

class Father():
    def say_6(self):
        print('666')

class Son(Father):
    def __init__(self):
        super(Son,self).say_6()
        super().say_6()
        Father.say_6(self)

son1=Son()
#666
#666
#666

  2.super (). Classification of property or method you want to tune

  3. class name. The properties and methods of the parent class you want to tune (self), multiple inheritance when the subclass, such methods can be selected parent class method in which a call.

  This is independent of a method and inheritance, the parent class is pure in memory.

Seven. Subclass initialization.

  After a further subclass overrides the parent class __init__ methods __init__ in a subclass you must use super method call parent class __init__, and pass parameters they need, otherwise they will lose the parent class certain features.

class Staff:
    def __init__(self, name, gender, age, wages):
        self.name = name
        self.gender = gender
        self.age = age
        self.wages = wages

class Programmer(Staff):
    def __init__(self, name, gender, age, wages,Skill):
        super().__init__(name, gender, age, wages)
        self.Skill=Skill

Eight. combination.

  When we need a function, and this function can not be obtained through inheritance, it can be used in combination.

  The composition is another class as the initial property of the incoming another class.

  For example, there is the hero league heroes, but also equipment, need to attack when the hero class needs to attack, equipped with attack, heroes need to get the attack and can not inherit the weapons because weapons and a hero is not a class, it can be the weapon as a property of the incoming class hero, when the need to attack the hero, a hero can be called weapons of attack, as follows, using simulated Galen Dolan played Raven 10:00 blood.

class Equipment:
    def __init__(self,name,attack):
        self.name=name
        self.attack=attack

    def staff(self,opp):
        opp.hp-=10

class Hero:
    def __init__(self,name,hp,equipment=1):
        self.name=name
        self.hp=hp
        self.equipment=equipment

duolan=Equipment('多兰',10)
garin=Hero('盖伦',100,duolan)
ruiwen=Hero('瑞文',100,0)
garin.equipment.staff(ruiwen)
print(garin.hp,ruiwen.hp)
#100,90

  Combination to further reduce the coupling ratio inheritance.

nine. Diamond inheritance

  When a class has more than one parent class, and more than one parent class have a common base class, which is called diamond inheritance.

  Added: new classes and Classic

  The new class: any display or implicitly called new class inherits from the class of the object (to python3 any of the classes are derived from the object class, python all new class)

  Classic: not the object of a subclass, only appear in python

  Supplementary graph traversal

  The picture shows the traversal order python3

The picture shows the traversal python3

The picture shows the traversal python3 and python2

When there is a diamond inheritance, the first new class of deep traversal, when faced with a common parent class on breadth

The new category is depth-first.

 

Guess you like

Origin www.cnblogs.com/LZXlzmmddtm/p/11247541.html