1)创建父类MyPrint类,包含show()方法,用于输出图形的形状。 (2)创建子类MyPrintSquare类,重写show ()方法,用“*”打印出边长为5的正方形。 (3)创建子类MyPrintCircle类,重写show ()方法, 用“*”打印出半径为5的圆。 (4)创建测试类,设计一个myshow(MyPrint a)方法,实现输出的功能:如果为MyPrintSquare, 输出边长为5的正方形,如果为MyPrintCircle对象,输出半径为5的圆;主函数中创建MyPrintSquare、MyPrintCircle的对象,分别调用myshow,检查输出结果。
这是基础java类定义的操作,请问题主遇到什么问题还是没有思路?
给个半成品代码,题主可以参考一下,这个就是按照题目要求设计出来的代码结构,就差具体的图形打印代码了
abstract class MyPrint{
abstract void show();
}
class MyPrintSquare extends MyPrint{
@Override
void show() {
//这里实现打印正方形的代码
}
}
class MyPrintCircle extends MyPrint{
@Override
void show() {
//这里实现打印圆形的代码
}
}
public class TestPrint {
public static void myshow(MyPrint a){
a.show();
}
public static void main(String[] args) {
MyPrintSquare mps = new MyPrintSquare();
MyPrintCircle mpc = new MyPrintCircle();
myshow(mps);
myshow(mpc);
}
}