Last active
February 2, 2018 09:51
-
-
Save spiderChow/2027cffcb9e131f563ec5047fc8e5bb2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # implement the static methods and class methods | |
| # use the decorator@ | |
| class MyClass: | |
| val1 = 'Value 1' | |
| def __init__(self): | |
| self.val2 = 'Value 2' | |
| @staticmethod | |
| def staticmd(): | |
| print '静态方法,无法访问val1和val2' | |
| @classmethod | |
| def classmd(cls): | |
| print '类方法,类:' + str(cls) + ',val1:' + cls.val1 + ',无法访问val2的值' | |
| """ 1)静态方法无需传入self参数,类成员方法需传入代表本类的cls参数; | |
| 2)从第1条,静态方法是无法访问实例变量的,而类成员方法也同样无法访问实例变量,但可以访问类变量; | |
| 3)静态方法有点像函数工具库的作用,而类成员方法则更接近类似Java面向对象概念中的静态方法。 | |
| """ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment