Inheritance supplement

What is inherited?

  It is a new type of way, inherit a class, class attributes and methods in the subclass

  The parent class is the base class, the derived class is a subclass

  Development: Development Tools IDE: vscode, pycharm, sumlinetext

       Development of Java: eclipse, myeclipse, idea

     Development Andrews: eclipse + adt, Androidstudio: based on the idea + adt reform

  The new class: As long as the object inherits class, is the new class, Python3 inherit the default object class, python2, we need to display the specified object inheritance

  Classic: not inherit object's class, is the classic category, Python3 no Classic, Python2 will have in

class A (Object): 
    Pass 
class B (A): 
    Pass 


# View class name 
print (B .__ name__) 


Output: B 

# View parent 
print (B .__ bases__) # B all the parent class 

output (<class' __main __. A '>,)

  

Second diamond property issues find:

people class 
    School = 'Peking University' 




class Teacher (people): 
    DEF the __init __ (Self, name, Age, Level) 
            the self.name name = 
            self.age = Age 
            self.level = Level 

class Student (people): 
    DEF the __init __ (Self , name, Age, Level) 
            the self.name name = 
            self.age = Age 
            self.level = Level 


STU1 student = ( 'bgon', 85, "Python") # instantiate an object 
print (stu1.school) # student class no school in this property, he defined in the parent class 



# output is: peking university

  If this is inherited, then the question becomes, how to reuse the properties of the parent class, to find what is the order of attributes, but also how to reuse the parent class method?

  We said before the order is to find objects: first find the object ===> class looking ===> parent class looking ===> error, but it is not the same as today, several inherited in the parent class, too that is, multiple inheritance, that is how to find?

III. Reuse parent class method

people class 
    School = 'Peking University' 
    DEF the __init __ (Self, name, Age, Level) 
            the self.name name = 
            self.age = Age 
            self.level = Level 



class Teacher (people): 
    DEF the __init __ (Self, name, Age, Level ) 
            the self.name name = 
            self.age = Age 
            self.level = Level 

class Student (people): 
    DEF the __init __ (Self, name, Age, Couse) 
            the self.name name = 
            self.age = Age 
            self.couse = Couse 


STU1 = student ( 'bgon', 85, "python") # instantiate an object 
print (stu1.school) # no school student class this attribute, he defined in the parent class

  As can be seen above, inheritance, code appears redundant, then how can we reuse the __init__ method of the parent class, let's look at the first method: using a naming names (nothing to do with inheritance)

class people
    school = 'peking university'
    def __init__(self, name, age, level)
            self.name = name
            self.age = age
            self.level = level

class Teacher(people):
    def __init__(self, name, age, level)
            self.name = name
            self.age = age
            self.level = level

class Student(people):
    def __init__(self, name, age, couse)
            people.__init__(self, age,name)
            self.couse = couse

  Second way: through the super keyword

class people
    school = 'peking university'
    def __init__(self, name, age, level)
            self.name = name
            self.age = age
            self.level = level

class Teacher(people):
    def __init__(self, name, age, level)
            self.name = name
            self.age = age
            self.level = level

class Student(people):
    def __init__(self, name, age, couse)
            super().__init__(name, age)
            self.couse = couse

  super () will get a list of the parent object in accordance with mro, binding method to call the object, need to pass the first parameter (Self), so there __init __ (name, age)

people class 
    School = 'Peking University' 
     DEF the __init __ (Self, name, Age) 
            the self.name name = 
            self.age = Agel 

class Teacher (people): 
    Pass 

class Student (people): 
     Pass 

STU1 Student = ( 'bgon', 85 ) # instantiate an object 
print (stu1.school) # class students in this school did not attribute his defined in the parent class

  Class instantiation automatically invokes __init__, is not a class, went to look for the parent class

多层继承:

class A:
    a = "AAAA"
pass class B(A): a = "BBBB"
  pass class C(B): a = "CCCC"
pass class D(C): a = "DDDD
   pass d = D() print(d, a) # 输出为DDDD

  

Multiple inheritance:

class A:
    a = "AAAA"
    pass
class B:
    a = "BBBB"
  pass
class C:
    a = "CCCC"
     pass
class D(A, B, C):
    a = "DDDD
   pass


d = D()
print(d, a)

  When the above search order that is how it? Or start your own first start looking, when D is not a value, the first find A, followed by a, B, C

Diamond inheritance problem (explicit inherit a class, not an object class):

  In order to find new classes and classic classes is not the same, the new type of search order is breadth-first (from the left ,, has been looking up to find the apex end diamond (not including diamond vertex), continue to the next succession father looking up to find the diamond-shaped apex end (not including the diamond-shaped vertex)), lookup order classic is a depth-first (from the left ,, has been looking up to find the diamond in the apex end (including diamond vertex)) ==> a road to find in the end and then go find the breadth

class A (Object): 
    DEF Test (Self): 
        Print ( 'from A') 

class B (A): 
    DEF Test (Self): 
        Print ( 'from B') 

class C (A): 
    DEF Test (Self): 
        Print ( 'from C') 

class D (B): 
    DEF Test (Self): 
        Print ( 'from D') 

class E (C): 
    DEF Test (Self): 
        Print ( 'from E') 

class F. (D, E): 
    # DEF the Test (Self): 
    # Print ( 'from F') 
    Pass 
f1 F = () 
f1.test () 
Print (F .__ mro__) # this attribute can only have a new linear list view, not classic this property is super strict accordance with mro find a list of 

# new class order of succession: F-> D-> B-> E-> C-> A 
# classic order of succession: F-> D-> B-> A- > E ->C 
# is a new class of unified python3
# Pyhon2 in Caifen new class of Classic

  

All are top parent, called diamond issue, that there is a general object to inherit

 

Guess you like

Origin www.cnblogs.com/panshao51km-cn/p/11617984.html