Inheritance and rewrite python class

Construction method:

def __init__(self)

Path: e: /pythonpro/liuyun/class02.py

# Constructor class ------ ------- 
class Person (): 
    name = " Han " 
    Sex = " M " 

    # construction method, a fixed wording: initializing a class 
    DEF  the __init__ (Self, XB ): 
        self.sex = XB 
        self.test () 
    
    DEF test (Self):
         Print ( " it is a test method " ) 

D = Person ( " F " )
 Print (d.name)
 Print (d.sex)

Class inheritance:

class subclass name (parent name):

Path: e: /pythonpro/liuyun/class03.py

# ------ class inheritance: the son inherited his father's legacy --------- 
# ------ rewrite: Rewrite can go to the parent class method, such as run () - ----- 

class Dongwu (): 
    name = " father " 
    TZ = 100 DEF RUN (Self):
         Print ( " ! animal run " )
     DEF Huxi (Self):
         Print ( " mammal breathing " ) class Ren (Dongwu):   # inherited from the parent animal 
    name = " son " # accounted pit, grammar does not complain, what is not is DEF run (Self):   # subclasses to override the parent class's run () method

    



            

    
        Print ( " people are walking upright " ) 

R & lt = Ren ()       # instantiated is a human, but who inherit properties and methods of animal 
Print (r.tz)     # subclass to call the parent class attributes 
r.run ()         # subclasses to call for subclasses 
r.huxi ()        # subclasses to call the parent's method

Guess you like

Origin www.cnblogs.com/minirabbit/p/12037088.html