想表达的都在代码上了,写完这个终于分清类属性、类方法、实例属性、实例方法啦!YES!
class Game:
#类属性 历史最高分
top_score=0
#初始化方法
def __init__(self,player_name):#player_name实例属性
self.player_name=player_name
#静态方法(不需要访问类属性和实例属性的方法) 帮助信息
@staticmethod
def show_help():
print("帮助信息...")
#类方法 显示历史最高分
@classmethod
def show_top_score(cls):
print("历史记录 %d" % cls.top_score)#访问类属性
#实例方法 开始游戏
def start_game(self):
print("%s 开始游戏..." % self.player_name)#访问实例属性
#查看游戏帮助信息 #调用静态方法
Game.show_help()#类名.方法名
#查看历史最高分 #调用类方法
Game.show_top_score()
#创建游戏对象
hh=Game("小花")
#调用对象实例方法
xh.start_game()
运行代码截图:
有什么错误或者建议欢迎提出,感谢!!!