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