The content of this section:
- Object-oriented Advanced Syntax section
- Static methods, class methods, property methods
- Special methods for classes
- Reflection
- Exception handling
- Socket Development Basics
Static methods
Its only association with a class is the need to call this method through the class name
#静态方法实际跟类没关系, there is no self variable.
#只是名义上贵归类管理, in fact, in a static method, you cannot access any property in a class or instance
1. When invoked, the instance itself is passed to the Eat method, i.e. D.eat (d)
2. Remove the self parameter in the Eat method, but this also means that you cannot pass self in eat. Calls to other variables in the instance
#!/usr/bin/env python#-*-coding:utf-8-*-#AUTHOR:DCCclassDog (object):def __init__(self,name): Self.name=namedefEat (Self,food):Print("%s is eatting%s"%(Self.name,food)) d= Dog ("Dog1") D.eat ("steamed Bun")#Static MethodsclassCat (object):def __init__(self,name): Self.name=name @staticmethod#static methods do not really matter with classes, there is no self variable. #just nominal expensive collation management, in fact, in a static method, cannot access any property in the class or instance defEat (Self,food):Print("%s is eatting%s"%(Self,food)) d= Cat ("Dog1") D.eat ("CAT1","Baozi")
Day7----Object-oriented programming advanced