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;
}
@Override
public double getPerimeter() {
// TODO Auto-generated method st
设计一个形状类(接口)Shape,方法:求周长和求面积
最新推荐文章于 2023-02-18 12:26:55 发布