设计几何图形(Shape)、矩形(Rectangle)、圆形(Circle)、正方形(Square),能够利用接口和多态性计算几何图形的面积和周长,并显示出来。
package lx_1;
public interface Shape {
public double cir = 0;
public double area = 0;
public double getCir();
public double getArea();
}
package lx_1;
public class Rectangle implements Shape{
private double wide;
private double older;
public void setWide(double wide) {
this.wide = wide;
}
public void setOlder(double older) {
this.older = older;
}
public double getCir() {
return 2 * (wide + older);
}
public double getArea() {
return wide * older;
}
}
package lx_1;
public class Circle implements Shape{
private final double PI = 3.14;
private double radius;
public void setRadius(double radius) {
this.radius = radius;
}
public double getCir() {
return 2 * PI * radius;
}
public double getArea() {
return PI * radius * radius;
}
}
package lx_1;
public class Square implements Shape{
private double side;
public void setSide(double side) {
this.side = side;
}
public double getCir() {
return 4 * side;
}
public double getArea() {
return side * side;
}
}
|
|