- Object-oriented Advanced Syntax section
- Static methods, class methods, property methods
- Special methods for classes
- Reflection
- Exception handling
- Socket Development Basics
1.1 Static methods, class methods, property methods
# Author alleyyu# static method
#
through@staticmethod adorners can be used to decorate the method into a static method, what is static method? In fact, it is not difficult to understand, the ordinary method can be called directly after instantiation, And in the method can pass self. Invokes an instance variable or a class variable, but a static method cannot access an instance variable or a class variable, and a method that cannot access an instance variable or a class variable is, in fact, nothing to do with the class itself, and its only association with a class is the need to call this method through the class name
#Class Dog (object):#def __init__ (Self,name,food):#Self.name=name#Self.__food=food#@ Staticmethod#Def eat (self):#Print ('%s is eating%s '% (Self.name,self.__food))###D1=dog (' Bark ', ' baozi ')#D1.eat (D1)#Class method #A is eating pizza class method is implemented by @classmethod adorners, the difference between class methods and ordinary methods is that class methods can only access class variables and cannot access instance variables#Class Dog (object):#Name= ' A '#Food= ' Pizza '#def __init__ (Self,name,food):#Self.name=name#Self.food=food#@ Classmethod#Def eat (self):#Print ('%s is eating%s '% (Self.name,self.food))###D1=dog (' Bark ', ' baozi ')#D1.eat ()#Property method#Class Dog (object):#def __init__ (Self,name,food):#Self.name=name#Self.__food=food#@ Property#Def eat (self):#Print ('%s is eating%s '% (Self.name,self.__food))#D1=dog (' Bark ', ' baozi ')#D1.eat#Print (Type (d1.eat), type (D1)) #<class ' Nonetype ', <class ' __main__. Dog ' >#Parameter of the property methodClassDog (object):Def__init__(Self,name,food): self.name=Name self.__food=Food @ PropertyDefEat (self):Print‘%s is eating%s'% (self.name,self.__food)) @eat. SetterDefEat (self, food):Print‘Set Value to food‘, food) self.__food =Food @eat. deleterDefEat (self):Del Self.__foodPrint"del the Attibute ) d1= Dog ( ' bark ", ' pizza " #传递参数时候调用setter d1.eat=pie del D1.eat #删除的时候调用deleterd1. Eat#print (Type (d1.eat), Type (D1)) #<class ' Nonetype ', <class ' __main__. Dog ';
Python Basics (7)