#类属性,实例属性
class Tool(object):
#类属性
num = 0
#方法
def __init__(self,new_name):
#实例属性
self.name=new_name
#类属性加一
Tool.num+=1
tool1=Tool('铁锹')
tool2=Tool('电钻')
tool3=Tool('斧子')
print(Tool.num)
#实例方法,类方法,静态方法
class Game(object):
#类属性
num=0
#实例方法
def __init__(self):
#实例属性
self.name='lueluelue'
#类方法(修改类属性的方法)
@classmethod
def add_num(cls):
cls.num=100
#静态方法
@staticmethod
def print_menu():
print('========================')
print('=========贪吃蛇=========')
print('========开始游戏========')
print('========结束游戏========')
print('========================')
#无论是类方法还是实例方法都必须要有一个参数,比如cls或self
#静态方法可以没有任何参数(也可以有参数)
game=Game()
Game.add_num()#通过类的名字直接调用类方法
game.add_num()#通过这个类创建的对象去调用这个类的方法
print(Game.num)
Game.print_menu()#通过类 去调用静态方法
game.print_menu()#通过实例对象 去调用静态方法
选择
#当想要对类中的属性进行操作,使用类方法
#当想要对实例属性进行操作,使用实例方法
#当想要进行的操作与类对象以及实例对象都没关系,可以用静态方法