PYTHON里同时运用实例方法、类方法和静态方法

2024-10-14 02:03:46

1、class Animal(object): result = 1 #首先创建一个类,然后我们定义一下类属性。

PYTHON里同时运用实例方法、类方法和静态方法

3、class Animal(object): result = 1 def __init__(self, name): self.name = name @staticmethod def welcome(): print("welcome to the Zoo!") #然后我们就可以设置一下静态方法了,静态方法不调用实例属性和类属性。

PYTHON里同时运用实例方法、类方法和静态方法

5、class Animal(object): result = 1 def __init__(self, name): self.name = name @staticmethod def welcome(): print("welcome to the Zoo!") @classmethod def total(cls): print("the total animal is %d." % cls.result) def play(self): print("playing with %s." % self.name) #最后就是创建实例方法,调用实例的属性。

PYTHON里同时运用实例方法、类方法和静态方法
猜你喜欢