赞
踩
- def 方法名(self,args): # args为参数
- pass
'运行
- class Animal(object):
- def eat(self,food):
- print(f"正在吃{food}")
- def play(self):
- print("正在玩耍")
- def sleep(self):
- print("正在睡觉")
- dog=Animal()
- dog.play() # 正在玩耍
'运行
Animal.play() # 报错,提示缺少self
Animal.play(self)
Animal.play(dog)
- class Animal(object):
- def eat(self,food):
- print(f"正在吃{food}")
- def play(self):
- print("正在玩耍")
- def sleep(self):
- self.eat("狗粮") # dog.eat("狗粮")
- print("正在睡觉")
- dog=Animal()
- dog.sleep()
'运行
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。