定义接口Animal,在接口中定义void Eat(String)
时间: 2024-03-06 18:51:56 浏览: 24
好的,以下是定义接口Animal,在接口中定义void Eat(String)的Java代码:
```java
public interface Animal {
void Eat(String food);
}
```
这个接口定义了一个Eat方法,它接收一个String类型的参数food,表示动物吃的食物。任何实现了Animal接口的类都必须实现Eat方法,否则编译会报错。
相关问题
定义接口Animal,在接口中定义void Eat(String)方法,使用Tiger类和Bird类实现Animal类,并调用子类覆盖父类Eat()方法
好的,以下是定义接口Animal,在接口中定义void Eat(String)方法,并使用Tiger类和Bird类实现Animal类,并调用子类覆盖父类Eat()方法的Java代码:
```java
public interface Animal {
void Eat(String food);
}
public class Tiger implements Animal {
@Override
public void Eat(String food) {
System.out.println("老虎正在吃" + food);
}
}
public class Bird implements Animal {
@Override
public void Eat(String food) {
System.out.println("鸟儿正在吃" + food);
}
}
public class Main {
public static void main(String[] args) {
Tiger tiger = new Tiger();
Bird bird = new Bird();
tiger.Eat("肉");
bird.Eat("虫子");
}
}
```
这个代码中,Animal接口定义了一个Eat方法,Tiger和Bird类都实现了Animal接口,并覆盖了Eat方法,分别输出了老虎正在吃和鸟儿正在吃的食物。在Main类中,我们创建了Tiger和Bird的实例,分别调用了它们的Eat方法,输出了对应的结果。
定义Bird类和Fish类实现Animal接口编写程序
好的,下面是Bird类和Fish类的定义,并且它们都实现了Animal接口的要求。
```java
// Animal接口
interface Animal {
void eat();
void sleep();
}
// Bird类实现Animal接口
class Bird implements Animal {
@Override
public void eat() {
System.out.println("Bird is eating.");
}
@Override
public void sleep() {
System.out.println("Bird is sleeping.");
}
public void fly() {
System.out.println("Bird is flying.");
}
}
// Fish类实现Animal接口
class Fish implements Animal {
@Override
public void eat() {
System.out.println("Fish is eating.");
}
@Override
public void sleep() {
System.out.println("Fish is sleeping.");
}
public void swim() {
System.out.println("Fish is swimming.");
}
}
// 测试程序
public class Main {
public static void main(String[] args) {
Bird bird = new Bird();
bird.eat();
bird.sleep();
bird.fly();
Fish fish = new Fish();
fish.eat();
fish.sleep();
fish.swim();
}
}
```
这段代码定义了一个Animal接口,以及Bird类和Fish类分别实现了该接口。在测试程序中,我们创建了一个Bird对象和一个Fish对象,并调用它们的eat()、sleep()方法,以及Bird特有的fly()方法和Fish特有的swim()方法。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)