学习笔记(08):21天通关Python(仅视频课)-类方法与静态方法

立即学习:https://edu.csdn.net/course/play/24797/282188?utm_source=blogtoedu

class Test:
    def __init__(self, name='test'):
        self.name = name
        print('init + ' + name)

    @classmethod
    def testA(cls):
        print('testA')

    @staticmethod
    def testB(v):
        print('testB + ' + v)


# 对象调用
t = Test()
t.testA()
# 类调用
Test.testA()
print("-" * 30)
# 对象调用
tt = Test()
tt.testB('val')
# 类调用
Test.testB('val')
print("-" * 30)
# 对象调用
ttt = Test()
print(ttt.name)
# 类调用
Test('val').name
'''
            实例方法    类方法     静态方法
对象调用    自动绑定    自动绑定    不自动绑定  
类调用     不自动绑定   自动绑定    不自动绑定
'''
发布了25 篇原创文章 · 获赞 4 · 访问量 608

猜你喜欢

转载自blog.csdn.net/happyk213/article/details/105139755
今日推荐