python面向对象应用-1
1 #猫 定义类
2 class Cat:
3 type = '猫'
4
5 #通过__init__初始化的特征
6 def __init__(self,nickname,age,color):
7 self.nickname = nickname
8 self.age = age
9 self.color = color
10 #函数
11 def eat(self,food):
12 print('{}喜欢吃{}'.format(self.nickname,food))
13
14 def catch_mouse(self,weight,color):
15 print('{},抓了一只{}kg的,{}的大老鼠!'.format(self.nickname,weight,color))
16
17 def sleep(self,hour):
18 if hour<5:
19 print('乖乖!继续睡觉!')
20 else:
21 print('赶快起床出去抓老鼠!')
22
23 def show(self):
24 print('猫的详细信息:')
25 print(self.nickname,self.age,self.color)
26
27 #调用函数,以及传参
28 cat1 = Cat('星星',1,'灰色')
29 cat1.catch_mouse(2,'蓝色')
30 cat1.sleep(9)
31 cat1.eat('小金鱼')
32 cat1.show()