用Java实现以下功能,3.父类MyPrint,包含show()方法,用于输出图形的形状。子类MyPrintSquare,重写 show()方法,打印出边长为5的正方形;子类MyPrintCircle,重写show()方法,打印出半径 为5的圆。测试类,设计一个myshow ( MyPrinta )方法,实现输出功能:如果为MyPrintSquare, 输出边长为5的正方形;如果为MyPrintCircle对象,输出半径为5的圆。主函数中创建MyPrintSquare 、MyPrintCircle的对象,分别调用myshow,检查输出结果。
时间: 2023-06-04 08:06:33 浏览: 237
这是一个计算机编程问题,属于技术范畴。以下是Java实现代码:
父类MyPrint:
```
public class MyPrint {
public void show() {
System.out.println("This is a shape");
}
}
```
子类MyPrintSquare:
```
public class MyPrintSquare extends MyPrint {
public void show() {
System.out.println("This is a square with side length of 5");
}
}
```
子类MyPrintCircle:
```
public class MyPrintCircle extends MyPrint {
public void show() {
System.out.println("This is a circle with radius of 5");
}
}
```
测试类:
```
public class Test {
public static void myshow(MyPrinta a) {
a.show();
}
public static void main(String[] args) {
MyPrintSquare square = new MyPrintSquare();
MyPrintCircle circle = new MyPrintCircle();
myshow(square);
myshow(circle);
}
}
```
输出结果为:
```
This is a square with side length of 5
This is a circle with radius of 5
```
阅读全文