Property:
Public properties (belonging to class, one copy per class)
Normal attribute (belongs to object, one copy per object)
Private property (object, similar to normal property, but not directly accessible by object)
Method: (by effect)
Construction method
Destructors
Method: (By Type)
Common methods
Private method (two underscore in front of method)
Static methods
Class method
Property method
Static methods
@staticmethod
Static methods, called directly through the class, do not need to create objects and do not implicitly pass the self
Class method
@classmethod
Class method, the self in the method is the class itself, and the value of the calling method simultaneous must also be the public property of the class.
This means that the class method can only manipulate the public fields of the class itself
Class Dog: Food = ' Gutou ' Age = ' 1 ' def __init__ (self, name): self.name = name @classmethod def Eat (self,age): #只能是类中的变量 # Print (self.name) print (age) print (self.food) @classmethod def eat1 (self, Age): # can only be a variable in a class # print (self.name) Age = "2" Self.food = "Tang" @ Staticmethod def print_1 (): print (Dog.food, dog.age) d = Dog ("Labuladuo") d.eat (dog.age) # Call Dog.eat (Dog.age) #通过类调用print via Object ( "-----1-----") D.EAT1 (Dog.age) dog.print_1 () print ("--------2-------") DOG.EAT1 (Dog.age) dog.print_1 ()
Property method
Property into a private property, plus assert
class Cycle (object): Def __init__ (self,x,y,radius): self.x = x sel F.y = y Self.radius = Radius @property def radius (self): return Self.__radius @radius. Setter def Radius (Self,radius): Assert radius > 0, "radius must be nonzero and non-negative" Self.__radius = Radius @radius. Deleter def radius (self): del Self.__radius def __str__ (self): return "({0},{1},{2})". Form at (Self.x,self.y,self.radius) c = Cycle (1,1,7) C.radius = 9print (c) del c.radiusprint (C.radius) # (1,1,9) #AttributeError : ' Cycle ' object has no attribute ' _cycle__radius '
Class Dog (object): def __init__ (self, name): self.name = name Self.__food = None # def eat (self, food): Original Way # Self.__food = food # print ('%s eat%s '% (self.name, food)) @property def Eat (self): print ('%s eat%s '% (Self.name,self.__food)) @eat. Setter def eat (self, food): self.__food = food @eat. Deleter def Eat (self): del self.__ Food print ("erased") d = Dog ("Labuladuo") # d.eat ("Baozi") #原始方式d. Eatd.eat = "Baozi" D.eat #调用方式没变, Just change the parameters passed in to change the result # can delete __food property del d.eatd.eat # error "Output Labuladuo eat Nonelabuladuo eat baozi deleted attributeerror: ' Dog ' Object has no attribute ' _dog__food '
Attribute Method Application Scenario
Well, what's the use of a method to make a static property? Since you want a static variable, it is directly defined as a static variable. Well, in the future you will need a lot of scenarios that cannot simply be achieved by defining static properties, for example, if you want to know the current state of a flight, whether it arrives, is delayed, canceled, or has flown away, you have to go through the following steps to understand this state:
1. Connecting Airlines API Query
2. Parsing the query results
3. Return the results to your users
So the value of this status property is a series of actions to get the result, so every time you call, it will go through a series of actions to return your results, but these action procedures do not require user care, the user only need to call this property can, understand?
Class Flight (object): def __init__ (self,name): self.flight_name = name def checking_status (self): Print ("Checking flight%s status"% self.flight_name) return 1 @property def flight_status (self): status = Self.checking_status () if status = = 0: print ("Flight got canceled ...") elif Status = = 1:
print ("Flight is arrived ...") elif Status = = 2: print ("Flight has departured already ...") else: Print ("Cannot confirm the flight status...,please check later") F = Flight ("CA980") f.flight_status
Cool, now I can only check flight status, since this flight_status is already a property, then I can assign it? Give it a try.
f = Flight ("CA980") F.flight_statusf.flight_status = 2
Output is: Discovery cannot be changed
Checking Flight CA980 Statusflight is arrived ... Traceback (most recent): File "/users/jieli/pycharmprojects/python Basic/Automation Day7 Object-oriented Advanced/attribute method. py", line +, in <module> f.flight_status = 2attributeerror:can ' t set attribute
Of course you can change it, but you need to decorate it with a @proerty.setter decorator, and you need to write a new method to change the flight_status.
Class Flight (object): Def __init__ (self,name): self.flight_name = name def checking_status (self): Prin T ("Checking flight%s status"% Self.flight_name) return 1 @property def flight_status (self): status = Self.checking_status () if status = = 0:print ("Flight got canceled ...") elif status = = 1: Print ("Flight is arrived ...") elif status = = 2:print ("Flight has departured already ...") Else:print ("Cannot confirm the flight status...,please check later") @flight_status. Setter #修改 def Flight_status (self,status): Status_dic = {: "Canceled",: "Arrived",: "Departured"} print ("\033[31;1MH As changed the flight status to \033[0m ", Status_dic.get (status)) @flight_status. Deleter #删除 def flight_status (self ): Print ("Status got removed ...") F = Flight ("CA980") F.flight_statusf.flight_status = 2 #触发 @flight_status. Setter de L F.flight_status #触发 @flIght_status.deleter
Python Learning note 8--Object-oriented--a detailed description of properties and methods