Python入门学习之:认识类方法

Python类实例方法:

和类属性一样,类方法可以分为类方法、实例方法和静态方法。

  •  @classmethod 修饰的方法为类方法;
  • 采用 @staticmethod 修饰的方法为静态方法;
  • 不用任何修改的方法为实例方法。
class TheFirstDemo:
    def __init__(self):     
        print("调用了")
    add = 'ziyuejiaoyu'
    @classmethod                        #类方法需要使用@classmethod修饰符  
    def say(self, content):
        print(content)
    @staticmethod                      #静态方法需要使用@staticmethod修饰
    def info(name,add):
        print(name,add)

Python 类方法和实例方法相似,它最少也要包含一个参数

静态方法没有类似 self、cls 这样的特殊参数,如此,类的静态方法中无法调用任何类属性和类方法。

发布了96 篇原创文章 · 获赞 76 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/u010244992/article/details/104911286
今日推荐