Python 学习 - 类方法

# coding:utf-8

# 类方法开头为@classmethod
# 参数为cls,调用类属性


class Tool:

    count = 0

    @classmethod
    def show_tool_count(cls):
        print '工具数量 %d' % cls.count


    def __init__(self, name):
        self.name = name

        Tool.count += 1

tool1 = Tool('ax')
tool2 = Tool('knife')

# 调用类方法
Tool.show_tool_count()




猜你喜欢

转载自blog.csdn.net/weixin_38892128/article/details/86546449