2021-08-26 Python之对象方法

#对象方法

 1 class person():
 2     name='张三'
 3     
 4     def __init__(self,name,age):
 5         self.age=age
 6         self.name=name
 7 
 8     def eat(self,food):
 9         self.food=food
10         print('{}正在吃{}'.format(self.name,self.food))
11 
12     def run(self):
13         print('{},今年{}岁,正在跑步'.format(self.name,self.age))
14 
15 p1=person('李斯',30)
16 p1.run()
17 p1.eat('红烧肉') #方法中有参数,在调用时需要传参。

 

posted @ 2021-08-26 13:51  admin-xiaoli  阅读(10)  评论(0编辑  收藏  举报