interface Control{ void control(); } class abb implements Control{ public void control(){ System.out.println("abb类下的对象a通过实现Control接口下的control方法打印:我属于abb,它实现了Control接口"); } } class abc implements Control{ public void control(){ System.out.println("abc类下的对象c通过实现Control接口下的control方法打印:我属于abc,它实现了Control接口"); } } public class Test{ public static void main(String[] args) { abb a=new abb(); a.control(); abc r=new abc(); r.control(); } }的输出结果?
时间: 2023-04-06 07:05:05 浏览: 70
我可以回答这个问题。输出结果为:
abb类下的对象a通过实现Control接口下的control方法打印:我属于abb,它实现了Control接口
abc类下的对象c通过实现Control接口下的control方法打印:我属于abc,它实现了Control接口
相关问题
public interface Plant { void eatable();} public class Grass implements Plant { @Override public void eatable() { System.out.println("Grass is eatable."); }}public class Tomato implements Plant { @Override public void eatable() { System.out.println("Tomato is eatable."); }} public class TestPlant { public static void main(String[] args) { Grass grass = new Grass(); Tomato tomato = new Tomato(); grass.eatable(); tomato.eatable(); }}的优化
这段代码本身没有明显的问题,但是可以进行一些优化:
1. 接口名可以更加明确,例如将 Plant 改为 Eatable。
2. 注释可以添加,例如给 Grass 和 Tomato 类添加注释说明它们的作用和实现原理。
3. 可以使用更加语义化的命名,例如将 TestPlant 改为 EatableTest。
修改后的代码如下:
```
public interface Eatable {
void isEatable();
}
public class Grass implements Eatable {
@Override
public void isEatable() {
System.out.println("Grass is eatable.");
}
}
public class Tomato implements Eatable {
@Override
public void isEatable() {
System.out.println("Tomato is eatable.");
}
}
public class EatableTest {
public static void main(String[] args) {
Grass grass = new Grass();
Tomato tomato = new Tomato();
grass.isEatable();
tomato.isEatable();
}
}
```
使用Java定义Shape接口实现多态。 interface Shape{ void draw(); void erase(); } Circle类、Square类、Triangle类实现Shape,编写程序NewShapes.java,利用Random类实现随机9次调用不同图形类绘图。Circle类的定义示例: class Circle implements Shape{ public void draw(){ System.out.println("Calling Circle.draw()"); } public void erase(){ System.out.println("Calling Circle.erase ()"); } }
下面是实现题目要求的Java代码:
```
import java.util.Random;
interface Shape {
void draw();
void erase();
}
class Circle implements Shape {
public void draw() {
System.out.println("Calling Circle.draw()");
}
public void erase() {
System.out.println("Calling Circle.erase()");
}
}
class Square implements Shape {
public void draw() {
System.out.println("Calling Square.draw()");
}
public void erase() {
System.out.println("Calling Square.erase()");
}
}
class Triangle implements Shape {
public void draw() {
System.out.println("Calling Triangle.draw()");
}
public void erase() {
System.out.println("Calling Triangle.erase()");
}
}
public class NewShapes {
public static void main(String[] args) {
Shape[] shapes = new Shape[9];
Random random = new Random();
for (int i = 0; i < shapes.length; i++) {
int shapeType = random.nextInt(3);
switch (shapeType) {
case 0:
shapes[i] = new Circle();
break;
case 1:
shapes[i] = new Square();
break;
case 2:
shapes[i] = new Triangle();
break;
}
}
for (Shape shape : shapes) {
shape.draw();
shape.erase();
}
}
}
```
上面的代码定义了一个 `Shape` 接口,其中包含了 `draw()` 和 `erase()` 方法。接着,`Circle`、`Square`、`Triangle` 类分别实现了 `Shape` 接口,并且实现了 `draw()` 和 `erase()` 方法。最后,`NewShapes` 类利用 `Random` 类生成随机数来创建不同的图形对象,并且调用这些图形对象的 `draw()` 和 `erase()` 方法。通过这个例子,我们可以看到多态的用法,即在运行时根据对象的实际类型来选择调用哪个方法。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)