当前位置:   article > 正文

Python类的实例方法、静态方法和类方法_python编写个人信息类的实例方法、类方法、静态方法。

python编写个人信息类的实例方法、类方法、静态方法。

Python类语法中支持三种方法:实例方法,静态方法,类方法。

class Foo(object):
    """类三种方法语法形式"""

    def instance_method(self):
        print("是类{}的实例方法,只能被实例对象调用".format(Foo))

    @staticmethod
    def static_method():
        print("是静态方法")

    @classmethod
    def class_method(cls):
        print("是类方法")

foo = Foo()
foo.instance_method()
foo.static_method()
foo.class_method()
print('----------------')
Foo.static_method()
Foo.class_method
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/903628
推荐阅读
相关标签
  

闽ICP备14008679号