24 面向对象综合1

class Game:

    top_score=0   #定义类属性

    @classmethod  #定义类方法,因为要调用类属性
    def show_top_score(cls):
        print("top score :%s "% cls.top_score)

    @staticmethod  #定义静态方法,什么都不需要调用
    def show_help():
         print("this is help")

    def __init__(self,player_name): #定义对象属性,传入对象属性,初始化
        self.player_name=player_name

    def start_game(self):  #定义对象方法,调用对象属性
        print("%s start game :"% self.player_name)

#调用类属性
print(Game.top_score)
#调用类方法
Game.show_top_score()
#调用类方法
Game.show_help()
#以上不需要创建实例

#创建对象
bill=Game("bill")
#调用对象方法
bill.start_game()

运行结果:

0
top score :0
this is help
bill start game :

 

posted @ 2020-06-12 00:57  abel2020  阅读(72)  评论(0编辑  收藏  举报