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

来来来交作业

package homework;
public abstract class shape {	
    public abstract void perimeter();		
    public abstract void area();
    }


public class Rectangle extends shape {

int h = 5;

int w = 3;

public void perimeter() {

System.out.println("矩形周长为:"+2*(h+w));

}


@Override

public void area() {

System.out.println("矩形面积为:"+(h*w));

}


}


public class Circle extends shape {

float r = 3.2f; 

float π = 3.14f;


@Override

public void perimeter() {

System.out.println("圆周长为:"+2*π*r);


}


@Override

public void area() {

System.out.println("圆面积为:"+r*π*r);

}


}

public class test {


public static void main(String[] args) {

Rectangle re = new Rectangle();

re.perimeter();

re.area();

Circle ci = new Circle();

ci.area();

ci.perimeter();

}


}


提问者:咕叽咕叽肉肉 2018-08-29 10:52

个回答

  • 尚雪莲
    2018-09-03 11:50:21

    Shape re = new Rectangle();

    re.peruneter();

    re.area();

    Shape ci = new Circle();

    ci.area();

    ci.peruneter();


    这个就是使用多态。

  • 志在成龙
    2018-09-01 19:48:39

    测试类要用多态吧!