java 定义一个矩形类,有长、宽2个属性,有成员函数计算矩形的面积。

2025-02-23 16:09:54
推荐回答(2个)
回答1:

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Rectangle R=new Rectangle(10,10);
System.out.println("面积为:"+R.Area());
}

}
class Rectangle{
public double width;
public double height;
Rectangle()
{
width=0;
height=0;
}
Rectangle(double w,double h)
{
width=w;
height=h;
}
public double Area()
{
return width*height;
}
}

回答2:

public class Rectangle {
private int w;
private int h;
public Rectangle(){
}
public Rectangle( int w, int h) {

this.w = w;
this.h = h;
}

public double area() {
double a=this.w;
double b=this.h;
return a*b;

}
}