python中 一个区分类属性,实例属性,静态方法,类方法,实例方法的案例 (Python经典编程案例)

it2022-05-05  126

代码如下:

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()

执行结果如下图:


最新回复(0)