python小猫吃鱼,喝水
时间: 2023-11-09 13:02:22 浏览: 315
小猫爱吃鱼。编写一个宠物类,以名字、体重、腿个数等属性进行初始化
回答:
可以用以下代码实现:
```
class Cat:
def __init__(self, name):
self.name = name
def eat(self, food):
print(f"{self.name} is eating {food}.")
def drink(self, drink):
print(f"{self.name} is drinking {drink}.")
cat = Cat("小猫")
cat.eat("鱼")
cat.drink("水")
```
输出结果为:
```
小猫 is eating 鱼.
小猫 is drinking 水.
```
阅读全文