zoukankan      html  css  js  c++  java
  • 类属性与类方法

    1.类属性-实例属性

    class Tool(object):

    #类属性
    num = 0

    #方法
    def __init__(self, new_name):
    #实例属性
    self.name = new_name
    #对类属性+=1
    Tool.num += 1


    tool1 = Tool("铁锹")
    tool2 = Tool("工兵铲")
    tool3 = Tool("水桶")

    print(Tool.num)

    2.实例方法-类方法-静态方法
    class Game(object):

    #类属性
    num = 0

    #实例方法
    def __init__(self):
    #实例属性
    self.name = "laowang"

    #类方法
    @classmethod
    def add_num(cls):
    cls.num = 100

    #静态方法
    @staticmethod
    def print_menu():
    print("----------------------")
    print(" 穿越火线V11.1")
    print(" 1. 开始游戏")
    print(" 2. 结束游戏")
    print("----------------------")

    game = Game()
    #Game.add_num()#可以通过类的名字调用类方法
    game.add_num()#还可以通过这个类创建出来的对象 去调用这个类方法
    print(Game.num)

    #Game.print_menu()#通过类 去调用静态方法
    game.print_menu()#通过实例对象 去调用静态方法
  • 相关阅读:
    rockGenmel stone.txt
    WHICHDAY.txt
    WORKDAYS.txt
    WAIT_YN.txt
    WEEKDAYS.txt
    WHEREXY.txt
    KeySelected.txt
    WINDOW.txt
    UPPER.txt
    ParentShapes It.txt
  • 原文地址:https://www.cnblogs.com/loser1949/p/9196218.html
Copyright © 2011-2022 走看看