2、定义一个矩形类Rectangle: [必做题]
2.1 定义三个方法:getArea()求面积、getPer()求周长,showAll()分别在控制台输出长、宽、面积、周长。
2.2 有2个属性:长length、宽width
2.3 通过构造方法Rectangle(int width, int length),分别给两个属性赋值
2.4 创建一个Rectangle对象,并输出相关信息
package Homework6_5;
public class Rectangle {
public double length;
public double width;
public Rectangle(double length, double width) {
this.length=length;
this.width=width;
}
public double getArea(double length,double width) {
return this.length*this.width;
}
public double getPer(double length,double width) {
return (this.length+this.width)*2;
}
public void showAll(double length,double width) {
System.out.println("长:"+this.length+"宽:"+this.width);
System.out.println("面积:"+getArea(this.length,this.width));
System.out.println("周长:"+getPer(this.length,this.width));
}
public static void main(String[] args) {
//创建对象
Rectangle rectangle=new Rectangle(4,3);
//引用方法
rectangle.showAll(rectangle.length,rectangle.width);
}
}