Methods in the class add the following adorner
Property Method: @property Change the method to a static property of the class, without parentheses when calling. To hide the implementation of the details, for the user, the feeling is to find a dead thing to write
classFlight (object):def __init__(self,name): Self.name=namedefcheck_flight (self):Print('Checking Flight%s:status'%self.name) Status= 3returnStatus @property#as a static property of a class, it is called without parentheses. To hide the implementation of the details, for the user, the feeling is to find a write dead static properties defFlight_status (self): status=self.check_flight ()ifStatus = =0:Print('flight got canceled ...') elifStatus = = 1: Print('flight is arrived ...') elifStatus = = 2: Print('flight has departured already ...') Else: Print('cannot confirm the flight status .... Please check later') @flight_status. Setter#Modify the parameters inside the property method defFlight_status (self,status): Status_dict={0:'canceled', 1:'arrived', 2:'departured' } Print('\033[31;1mhas changed flight:%s status to:%s\033[0m'%(Self.name,status_dict[status])) @flight_status. Deleter#This static method can be deleted by adding the adorner. defFlight_status (self):Print('status Func got removed') F1= Flight ("Chuan Hang 3u8633") F1.flight_status#no parenthesesF1.flight_status = 1F1.flight_statusdelF1.flight_statusf1.flight_status
View Code
static method: @staticmethod
Only nominal collation management, in fact, in the static method can not access any property of the class or instance
classDog (object):def __init__(self,name): Self.name=namedefTalk (self):Print('%s talking: Chewy! '%self.name) @staticmethoddefEat (Self,food):Print('%s Eating%s'%(Self.name,food)) D1= Dog ('Chenronghua') D1.eat ('steamed Bun')
View Code
Traceback (most recent): File "c:/users/administrator/desktop/python3_study/day6/static Method _ class method. py", line 15, In <module> d1.eat (' Bun ') typeerror:eat () Missing 1 required positional argument: ' Food '
Class method: @classmethod
Only class variables can be accessed and instance variables cannot be accessed
classDog (object): Name='Class method call Test with class variable' def __init__(self,name): Self.name=namedefTalk (self):Print('%s talking: Chewy! '%self.name)#@staticmethod@classmethoddefEat (Self,food):Print('%s Eating%s'%(Self.name,food)) D1= Dog ('Chenronghua') D1.eat ('steamed Bun')
View Code
class method call test class variable eating bun process finished with exit code 0
Python property methods, static methods, class methods