方法综合演练
需求
-
设计一个game类
-
属性:
- 定义一个类属性
top_score
记录游戏的历史最高分 - 定义一个实例属性
player_name
记录当前游戏的玩家姓名
- 定义一个类属性
-
方法:
- 静态方法
show_help
显示游戏帮助信息 - 类方法
show_top_score
显示历史最高分 - 实例方法
start_game
开始当前玩家的游戏
- 静态方法
-
主程序步骤
- 查看帮助信息
- 查看历史最高分
- 创建游戏对象,开始游戏
class Game(object):
# 创建类属性 - 历史最高分
top_score = 0
# 创建实例属性 - 玩家姓名
def __init__(self,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)
# 1. 查看游戏帮助信息
Game.show_help
# 2. 查看历史最高分
Game.show_top_score()
# 3. 创建游戏对象
game = Game("小明")
game.start_game()
输出为