static methods (Staticmethod nominal collation management, in which static properties in a class or instance are not actually accessed in a static method)
1 classDays (object):2 def __init__(self, food):3Self.food = Food4 5@staticmethod#actual and class are not related6 defTell (self):7 Print('there's%s,%s here!'% (Self.food,'name'))8 9 TenA = Days ('Banana') OneA.tell (a)class method (Classmethod can only access class variables and cannot access instance variables)
1 lass F2 (object):2Name ='Big Cellular' #class Variables3 4 def __init__(self, name):5Self.name =name6 7 @classmethod8 defEat (self):9 Print('There is%s, "%s"'% (Self.name,'methods of the class'))Ten One AF2.eat ()attribute method (turns a method into a static property)
The definition and invocation of a property is a few points to note:1 when defining, add a @property adorner on the basis of a common method;2 when the property has only one self parameter , 3 is called. There are two ways to define a property without parentheses:1 Adorner: applying adorner 2 to a method static field: Defining a static field in a class with a value
Classic class, with a @property adorner:
1 classF3 (object):2 def __init__(self, name):3Self.name =name4Self.__food=None5 6 @property7 defeat2 (self):8 Print('here%s, "%s"'% (Self.name,'What's going on'))9 Ten Onec = F3 ('Ah, pig .') AC.eat2The new class, with three kinds of @property decorators, corresponds to three @property, @ method name. Setter, @ Method name. Deleter the method to be modified.
1 classF4 (object):2 def __init__(self, name):3Self.name =name4Self.__food=None5 6@property#turn a normal method into a property method to get7 defEat (self):8 Print('here%s, "%s"'% (Self.name, self.__food))#since there is no parameter Self.__food in this call9 Ten@eat. Setter#Modify One defeat (self, food): A Print('I want to eat%s'%Food ) -Self.__food= Food - the@eat. Getter#Delete - defEat (self): - delSelf.__food - Print('Delete field Self.__food') + - +D = F4 ('Ho') A d.eat atD.eat ='dumplings' - deld.eat -D.eat
Python object-oriented static methods, class methods, property methods