python:classmethod 的使用

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
class A(object):
    bar = 1
    def func1(self):  
        print ('foo') 
    @classmethod
    def func2(cls):
        print ('func2')
        print (cls.bar)
        cls().func1()   # 调用 foo 方法
 
A.func2()               # 不需要实例化

输出结果为:

func2
1
foo

猜你喜欢

转载自blog.csdn.net/rocling/article/details/89978198
今日推荐