按以下要求编写程序: (1) 编写Plant接口,接口中声明eatable()方法 (2) 定义Grass类和Tomato类实现Plant接口 (3) 编写Grass类和Tomato类的测试类,并调用其中的eatable()方法,输出相关信息。
时间: 2023-07-14 13:14:13 浏览: 229
以下是Java语言的代码实现:
Plant接口:
```java
public interface Plant {
void eatable();
}
```
Grass类:
```java
public class Grass implements Plant {
@Override
public void eatable() {
System.out.println("This grass is eatable.");
}
}
```
Tomato类:
```java
public class Tomato implements Plant {
@Override
public void eatable() {
System.out.println("This tomato is eatable.");
}
}
```
测试类:
```java
public class Test {
public static void main(String[] args) {
Plant grass = new Grass();
Plant tomato = new Tomato();
grass.eatable();
tomato.eatable();
}
}
```
输出结果:
```
This grass is eatable.
This tomato is eatable.
```
阅读全文