黑马程序员技术交流社区
标题:
接口与实现类
[打印本页]
作者:
仲德明
时间:
2015-6-4 23:36
标题:
接口与实现类
设计几何图形(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;
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2