Python中类的静态函数、静态变量的实现

# encoding:UTF-8
__author__ = 'mrsimple'

# 含有类的静态方法、静态变量, 普通类成员函数, 成员变量
class People:
    count = 0
    sum = 0
    def __init__(self, name, age, gend):
        self.name = name
        self.age = age
        self.gender = gend
        People.peopleCount()    # 人数计数
        People.ageSum(age)      # 岁数计数

    def toString(self):
        print "name : %s, age : %d, gender : %s." % (self.name,self.age, self.gender)

    # 静态函数
    @staticmethod
    def sayHi(fname):
        print "hi," + fname

    # 间接实现静态成员变量
    @classmethod
    def peopleCount(cls):
        cls.count += 1
        return cls.count

    # 间接实现静态成员变量, 有参数
    @classmethod
    def ageSum(cls, age):
        cls.sum += age
        return cls.sum

ple = People("mr.simple", 22, "male")
ple.toString()
People.sayHi("frank")
print ple.name

print  People.peopleCount()
print  People.ageSum(10)

使用@staticmethod标签来表明函数为类的静态函数,且sayHi的参数中没有self。而类的静态变量借助@classmethod来实现,其中peopleCount中为无参静态函数,而ageSum中有一个age参数。

如图 : 

Python中类的静态函数、静态变量的实现_第1张图片

你可能感兴趣的:(Python)