问答详情
源自:10-3 Java 中的抽象类

【交作文 】大家看看还需要哪些改进 小白求教@!!!

package com.imooc;

public abstract class Shape {

public abstract void hello (String girth,String area);

}

-------------------------------------------------

package com.imooc;

public class Rectangle extends Shape {

@Override

public void hello(String girth,String area) {

// TODO Auto-generated method stub

System.out.println("矩形的周长是:"+girth+",面积是:"+area);

}

}

--------------------------------------------------------

package com.imooc;

public class Circle extends Shape {

@Override

public void hello(String girth, String area) {

// TODO Auto-generated method stub

System.out.println("圆形的周长是:"+girth+",面积是:"+area);

}

}

------------------------------------------------------------

package com.imooc;

public class Text {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

Shape rec=new Rectangle();

rec.hello("2(a+b)", "a*b");//a为矩形的长,b为矩形的宽。

Shape cir=new Circle();

cir.hello("2Math.PIr","Math.PIr²" );

}

}

-----------------------------------------------------

矩形的周长是:2(a+b),面积是:a*b

圆形的周长是:2Math.PIr,面积是:Math.PIr²


提问者:慕村2485551 2018-09-13 00:19

个回答

  • 慕丝8486895
    2018-09-15 11:27:22

    感觉应该是把周长方法和面积方法先抽象化,再在子类里面分别实现比较好