以上的代码要求输出大花猫喵喵叫 大花猫吃鱼 大花猫6岁 大黑狗汪汪叫 大黑狗吃骨头 大黑狗8岁
时间: 2023-11-15 08:56:20 浏览: 79
好的,以下是对应的代码:
```python
class Animal:
def __init__(self, name, sound, food, age):
self.name = name
self.sound = sound
self.food = food
self.age = age
def make_sound(self):
print(self.name + self.sound)
def eat(self):
print(self.name + self.food)
def get_age(self):
print(self.name + str(self.age) + "岁")
cat = Animal("大花猫", "喵喵叫", "吃鱼", 6)
dog = Animal("大黑狗", "汪汪叫", "吃骨头", 8)
cat.make_sound()
cat.eat()
cat.get_age()
dog.make_sound()
dog.eat()
dog.get_age()
```
输出结果:
```
大花猫喵喵叫
大花猫吃鱼
大花猫6岁
大黑狗汪汪叫
大黑狗吃骨头
大黑狗8岁
```
相关问题
本案例要求用java编写一个程序模拟饲养员喂食动物的过程,案例要求如下: 饲养员给小狗喂骨头,小狗汪汪叫。 饲养员给小猫喂小鱼,小猫喵喵叫
好的,我理解了你的要求。以下是Java代码实现:
```java
public class AnimalFeeding {
public static void main(String[] args) {
Animal dog = new Dog();
Animal cat = new Cat();
Feeder feeder = new Feeder();
feeder.feed(dog, new Bone());
feeder.feed(cat, new Fish());
}
}
interface Animal {
void eat(Food food);
String getSound();
}
class Dog implements Animal {
public void eat(Food food) {
System.out.println("小狗在啃骨头。");
}
public String getSound() {
return "汪汪";
}
}
class Cat implements Animal {
public void eat(Food food) {
System.out.println("小猫在吃小鱼。");
}
public String getSound() {
return "喵喵";
}
}
interface Food {}
class Bone implements Food {}
class Fish implements Food {}
class Feeder {
public void feed(Animal animal, Food food) {
animal.eat(food);
System.out.println(animal.getSound() + "叫。");
}
}
```
在上面的代码中,我们定义了三个接口和三个类。`Animal`接口定义了动物的基本行为,包括吃和叫两个方法。`Dog`和`Cat`类实现了`Animal`接口,并重写了`eat`和`getSound`方法。`Bone`和`Fish`类分别实现了`Food`接口,表示饲养员给动物喂的食物。`Feeder`类表示饲养员,其中的`feed`方法接收一个`Animal`对象和一个`Food`对象,调用动物的`eat`方法,然后输出动物的叫声。
在`main`方法中,我们创建了一个`Dog`对象和一个`Cat`对象,然后创建了一个`Feeder`对象并调用其`feed`方法,分别给动物喂食,输出了动物的叫声。
当我们运行这个程序时,会输出以下内容:
```
小狗在啃骨头。
汪汪叫。
小猫在吃小鱼。
喵喵叫。
```
这表示程序已经成功模拟了饲养员给小狗喂骨头、小狗汪汪叫和饲养员给小猫喂小鱼、小猫喵喵叫的过程。
1.编写Animal的实现类Cat,重写shout方法,打印输出:"小花猫喵喵叫" 2.编写Animal的实现类Dog,重写shout方法,打印输出:"大黄狗汪汪叫" 3.编写Animal的实现类Pig,重写shout方法,打印输出:"大胖猪喊喊叫"
我明白了,下面是我对你的回答:
1. 编写Animal的实现类Cat,重写shout方法,打印输出:"小花猫喵喵叫":
```java
public class Cat implements Animal {
@Override
public void shout() {
System.out.println("小花猫喵喵叫");
}
}
```
2. 编写Animal的实现类Dog,重写shout方法,打印输出:"大黄狗汪汪叫":
```java
public class Dog implements Animal {
@Override
public void shout() {
System.out.println("大黄狗汪汪叫");
}
}
```
3. 编写Animal的实现类Pig,重写shout方法,打印输出:"大胖猪喊喊叫":
```java
public class Pig implements Animal {
@Override
public void shout() {
System.out.println("大胖猪喊喊叫");
}
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)