Object-oriented acquaintance python3

"" " 
Syntax: 
class name of the class 
drawings is a class, according to the drawings each vehicle is made out of an object. 

1, attributes: object can describe a variable (variable) 
action can be described in one object can perform: 2, Action (function) 

in access class attributes:. self property
class access operation:. self action
"" " class Car: DEF the __init__ (Self, Color): # Self is the object self.color Color = # attributes DEF RUN (Self ): # action Print ( " my car will run " ) # create objects init initialization factory setting c1 = car ( " Red " ) # class name () => default actually executed is __init__ function c2 = car ( "white")

 

self in the end is what the hell?

class Car:
     DEF  __init__ (Self, Color):   # Self is the object 
        Print (Self)   # Self in the end is what ghost 
        self.color Color =   # attribute 

    DEF RUN (Self):   # Action 
        Print ( " My car will run ." )

 
Car = C1 ( " Red " )  
 Print (C1)

Results of the:

<__main__.Car object at 0x000000000228CEB8>
<__main__.Car object at 0x000000000228CEB8>

 Conclusion: self is the object

 

Different objects calls the same method, there will be what kind of effect na?

class Car:
     DEF  __init__ (Self, Color):   # Self is the object 
        Print (Self)   # Self in the end is what ghost 
        self.color Color =   # attribute 

    DEF RUN (Self):   # Action 
        Print (f " My {self.color } car will run " )

 
C1 = car ( " Red " )   
C2 = car ( " White " ) 
c1.run () 
c2.run ()

Results of the:

< __Main__ .car Object AT 0x00000000029997F0> 
< __main__ .car Object AT 0x0000000002999828> 
my red car will run 
my white car will run

Conclusion: The different objects of the same property or method call, the call is the object's own properties and methods.

 

 

Guess you like

Origin www.cnblogs.com/lilyxiaoyy/p/11988523.html