python_ class method with parameters

Method class parameters

1, the constructor arguments, parameters need instantiation

class the Person (): # define classes 
    DEF  __init__ (Self, name, leg = 8): # constructor parameters 
        self.name = name 
        self.leg = leg
     DEF RUN (Self):
         Print ( " I will run " )
         Print (self.name)
         Print (self.leg)
     DEF Fly (Self):
         Print ( " I can fly " ) 

ZWJ = the Person ( " Xiaomei " ) # required parameters when instantiating 
zwj.run ()

2, not constructor parameters, examples of a method with parameters, without instantiation parameters, parameters to a method call

class the Person (): # define classes 
    DEF  the __init__ (Self): # constructor with no arguments 
       Print ( " JJJ " )
     DEF RUN (Self, name, leg =. 8): # example method parameters 
        Print ( " I will run ." )
         Print (name)
         Print (leg)
     DEF Fly (Self):
         Print ( " I can fly " ) 

ZWJ = the Person () # instantiation need not take arguments 
zwj.run ( " Xiaomei " ) #Retrieval method requires parameters

 

Guess you like

Origin www.cnblogs.com/xiaokuangnvhai/p/11201312.html