首页 > 其他 > 详细

有关类方法属性,静态方法,实例方法属性

时间:2019-04-07 11:21:45      阅读:125      评论:0      收藏:0      [点我收藏+]
class Game():
    # 这是个类属性
    top_fen = 0

    @classmethod
    # 这是类方法
    def show_top_fen(cls):
        print("最高分是%d,这是个类方法" % cls.top_fen)

    @staticmethod
    # 这是个静态方法
    def show_help():
        print("这个是个静态方法,不调用类属性和方法,也不调用实例属性和方法")

    def __init__(self, name):
        self.name = name
        print("%s这是实例属性" % self.name)

    def play_game(self):
        print("%s开始游戏了,这是个实例方法" % self.name)


game = Game("‘传入的名字‘")
print(game)
print(Game.top_fen)
Game.show_top_fen()
Game.show_help()
game.play_game()

 

有关类方法属性,静态方法,实例方法属性

原文:https://www.cnblogs.com/xusuns/p/10664228.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!