java编程 请问有人会这道题吗 帮帮忙谢谢 10
2020-04-14
1、代码如下
Rectangle类
package baidu.zhidao;
/**
* Rectangle
* @author bufei
*/
public class Rectangle {
private int length;
private int width;
public int getArea() {
int area = this.length * this.width;
return area;
}
public int getPerimeter() {
int perimeter = 2 * (this.length + this.width);
return perimeter;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public Rectangle(int length, int width) {
this.length = length;
this.width = width;
}
@Override
public String toString() {
return "Rectangle [length=" + length + ", width=" + width + "]";
}
}
test类:
package baidu.test;
import baidu.zhidao.Rectangle;
public class Test{
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(20, 40);
System.out.println(rectangle.toString());
System.out.println("长方形,长 " + rectangle.getLength() + ",宽 " + rectangle.getWidth());
System.out.println("长方形面积:" + rectangle.getArea());
System.out.println("长方形周长:" + rectangle.getPerimeter());
}
}
2、运行