求解一道简单的java编程题目

来源:百度知道 编辑:UC知道 时间:2024/06/05 17:18:14
public class TestMyRectangle{
public static void main(String args[]){
MyRectangle obj=new MyRectangle(100,100,300,200);
System.out.println(obj.tostring());
}
}
class MyRectangle{
private int xUp,yUp,xDown,yDown,W,H,A;
MyRectangle(){}
MyRectangle(int xUp,int yUp,int xDown,int yDown){
this.xUp=xUp;
this.yUp=yUp;
this.xDown=xDown;
this.yDown=yDown;

}
void getW(int xDown,int xUp){
W=xDown-xUp;
}
void getH(int yDown,int yUp){
H=yDown-yUp;
}
void area(){
A=W*H;
}
String tostring(){
return "矩形的宽:"+W+"高:"+H+"面积:"+W*H;
}
}
为什么输出总是0啊?请教各位java高手
那应该怎么写啊?请教

void getW(int xDown,int xUp){
W=xDown-xUp;
}
void getH(int yDown,int yUp){
H=yDown-yUp;
}
void area(){
A=W*H;
}
这三个方法你都没调用,当然是0

public class TestMyRectangle{
public static void main(String args[]){
MyRectangle obj=new MyRectangle(100,100,300,200);
System.out.println(obj.tostring());
}
}
class MyRectangle{
private int xUp,yUp,xDown,yDown,W,H,A;
MyRectangle(){}
MyRectangle(int xUp,int yUp,int xDown,int yDown){
this.xUp=xUp;
this.yUp=yUp;
this.xDown=xDown;
this.yDown=yDown;
W=xDown-xUp;
H=yDown-yUp;
A=W*H;
}
String tostring(){
return "矩形的宽:"+W+"高:"+H+"面积:"+W*H;
}
}

因为你写了方法但是却没有用这些方法