python中的静态类与方法

来源:互联网 发布:linux下载mysql的命令 编辑:程序博客网 时间:2024/05/22 04:51

觉得语法啥限制的没有java严格,静态方法python的语法默认是这样子的:

    @staticmethod    def s():        pass

其实你把cls加进来就能调用静态变量,把self加进来就能调用自己的变量了,看看混乱的代码吧,其实对理解也有好处,静态变量和类变量的名字都一样。amazing

class TestClassMethod:    def __init__(self):        TestClassMethod.s=3;        self.s=2    def add(self,cls):        self.s=self.s+1;    @classmethod    def anotheradd(cls,self):        cls.s=s.s+1    @staticmethod    def otheradd(self,cls):        print "as",self.s        TestClassMethod.s=TestClassMethod.s+1    @staticmethod    def s():        passs=TestClassMethod()s.anotheradd(s)TestClassMethod.anotheradd(s)TestClassMethod.otheradd(s,TestClassMethod)print s.sprint TestClassMethod.s


0 0
原创粉丝点击