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" % Game.top_score)
print("历史最高分:%d" % cls.top_score)
# 实例方法
def start_game(self):
print("%s开始游戏" % self.player_name)
# 调用静态方法
Game.show_help()
# 调用类方法
Game.show_top_score()
# 调用实例方法
game = Game("张三")
game.start_game()
"""
确定方法类型的套路
1.实例方法:方法内部需要访问实例属性
实例方法内部可以使用"类名."访问类属性
2.类方法:方法内部只需要访问类属性
3.静态方法:方法内部不需要访问实例属性和类属性
"""
python方法综合案例
最新推荐文章于 2024-07-20 17:12:48 发布