package changfangxing;
public class Rectangle{
double length;
double width;
public Rectangle(int width,int length){
this.length=length;
this.width=width;
}
public double getArea(int length,int width){
return this.length*this.width;
}
public double getPer(int length,int width){
return 2*(this.length+this.width);
}
public void showAll(){
System.out.println("长"+this.length+","+this.width);
System.out.println("面积是"+this.length*this.width);
System.out.println("周长是"+2*(this.length+this.width));
}
}
package changfangxing;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Rectangle r=new Rectangle(2,4);
r.showAll();
}
}
定义一个矩形类Rectangle: [必做题] 2.1 定义三个方法:getArea()求面积、getPer()求周长,showAll()分别在控制台输出长、宽、面积、周长。 2.2 有2个属性:
最新推荐文章于 2024-06-04 17:42:25 发布