设计一个形状类(接口)Shape,方法:求周长和求面积
形状类(接口)的子类(实现类):
package zuoye2; public interface Shape { // 求面积方法 double getArea(); // 求周长方法 double getPerimeter(); }
Rect(矩形)
package zuoye2; public class Rect implements Shape { private double width; // 宽 private double height; // 高 public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } // 一个参数构造,给子类正方形用 public Rect(double width) { this.width = width; } //两个参数的构造,自己用 public Rect(double width, double height) { this.height = height; this.width = width; } @Override public double getArea() { // TODO Auto-generated method stub return width * height; }