咕叽咕叽肉肉
2018-08-29 10:52
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();
}
}
Shape re = new Rectangle();
re.peruneter();
re.area();
Shape ci = new Circle();
ci.area();
ci.peruneter();
这个就是使用多态。
测试类要用多态吧!
Java入门第二季 升级版
530559 学习 · 6091 问题
相似问题