JAVA:定义矩形Rectangle定义矩形Rectangle ,矩形信息包括宽 高 提供含两个参数

发布时间:2021-02-26 06:43:41

JAVA:定义矩形Rectangle定义矩形Rectangle ,矩形信息包括宽 高 提供含两个参数的构造方法,为字段提供get和set方法.提供计算面积的calcArea方法,提供计算周长calcCircum方法,重写equals方法.面积相等则相等.

网友回答

public class Rectangle {
private float length;
private float width;
public Rectangle(float length, float width) {
this.length = length;
this.width = width;
}public float calcArea() {
return this.length * this.width;
}public float calcCircum() {
return (this.length + this.width) * 2;
}@Override
public int hashCode() {
return new Float(this.length * this.width).hashCode();
}@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}if (obj == null || !(obj instanceof Rectangle)) {
return false;
}Rectangle rectangle = (Rectangle) obj;
if (this.calcArea() == rectangle.calcArea()) {
return true;
}return false;
}public float getLength() {
return length;
}public void setLength(float length) {
this.length = length;
}public float getWidth() {
return width;
}public void setWidth(float width) {
this.width = width;
}}
以上问题属网友观点,不代表本站立场,仅供参考!