分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow
# 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.sumple = People("mr.simple", 22, "male")ple.toString()People.sayHi("frank")print ple.nameprint People.peopleCount()print People.ageSum(10)
使用@staticmethod标签来表明函数为类的静态函数,且sayHi的参数中没有self。而类的静态变量借助@classmethod来实现,其中peopleCount中为无参静态函数,而ageSum中有一个age参数。
如图 :
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow