three characteristics of object-oriented python - Inheritance

# When inheritance 
# when significantly different between when the class 1, the class and the smaller the larger class components required, in combination with better 
# many of the same features as between 2 classes, provided these features make common base class, inheritance better 


class Dad: 

    " this is like my father " 
    Money = 10 DEF __init__ (Self, name):
         Print ( " Daddy " ) 
        self.name = name DEF hit_son (Self):
         Print ( " % S is playing son " % the self.name) class son (Dad): 
    Money = 10000000
     # when the sub-class has attributes with data properties of the parent class, or the same name as the parent class overrides property attributes, when no priority will call the parent class method when the child class

     

    



DEF __init__ (Self, name, Age): self.name = name self.age = Age DEF hit_son (Self): Print ( " from the son of the class " , self.name) # Print (Son.money) # Son.hit_son ( ) # Print (Dad .__ dict__) # Print (son .__ dict__) # s1 = son ( "TANG") # # Print (s1.name) # Print (s1.money) # s1.hit_son () # now look subclass own init method s1 = Son ( " alex " , "18") s1.hit_son() # print(Son.money) # print(Dad.money)




# Abc module implementation using a subclass of the base class must implement
abc Import 
# interface inheritance

class All_file (the metaclass that = abc.ABCMeta):
@ abc.abstractmethod

DEF the Read (Self):
Pass

@ abc.abstractmethod
DEF the Write (Self):
Pass


#: Interface is a function, interface inheritance: the definition of a base class, the base class for them to define their own methods to the base class function to a subclass inherits a method he would have to implement the base class
# using the abc module for sub-class must implement the method of the base class


class Disk (All_file):

DEF the Read (Self):
Print ( "Disk the Read")

DEF the Write (Self):
Print ( "Disk the Write")


class Cdroom (All_file):
DEF the Read (Self):
Print ( "cdrom the Read")

DEF the Write (Self):
Print ( "the Write cdrom")


class MEm (All_file):
DEF the Read (Self):
Print ( "Mem read")


the Write DEF (Self):
Print ( "the Write Mem")

DEF the SAD (Self):
Print ( "jjj")


M1 = MEm ()
# m1.sad ()
# m1.read ()
# m1.write ()




# Inherited sequence: 1.python2: new classes and sub classic classic means not object class inherits the base class, the base class for the new class inherits class object
2.python3: default inherit the new class object which is the default base class
3. classic default following is limited to a depth of the order, the new class is the default breadth-first order of succession, the new class can use the class attribute .__ mro__ like to see the order of succession
class A: 
DEF Test (Self):
Print ( "A")

# When the class is a new class inherits the search sequence for the breadth-first

class B (A):
DEF Test (Self):
Print ( "B")


class C (A ):
DEF Test (Self):
Print ( "C")

class D (B):
DEF Test (Self):
Print ( "D")


class E (C):
DEF Test (Self):
Print ( "E")


F. class (D, E):
Pass
# DEF Test (Self):
# Print ( "F.")

F1 = F. ()
f1.test () F. # ---> D ----> ---- B > E ----> C ----> a

Print (F .__ mro__)
#python in the end is how to achieve inheritance, for each class you define, python will calculate a method resolution order (MRO) Ganso, this MRO
# list is a simple linear sequence of tuples base class, the base class object inherits default to python3



 

 

Guess you like

Origin www.cnblogs.com/tangcode/p/11287046.html