public class Box { int x1,y1,x2,y2=0; Box buildBox(int x1,int y2,int x2,int y2){ this.x1=x1; this.y1=y1; this.x2=x2; this.y2=y2; return this; } Box buildBox(Point topLeft,Point bottomRight){ x1=topLeft.x; y1=topLeft.y; x2=bottomRight.x; y22=bottomRight.y; return this; } Box buildBox(Point topLeft,int w,int h){ x1=topLeft.x; y2= topLeft.y; x2=(x1+w); y2=(y1+h); return this; } void printBox(){ System.out.println("Box:<"+x1+","+y1); System.out.println(","+x2+","+y2+" > "); } public static void main(String[] args) { rect.buildBox(25,25,50,50); rect.printBox(); rect.buildBox(new Point(10,10),new Point(20,20)); rect.printBox(); rect.buildBox(new Point(10,10),50,50); rect.printBox(); } }
时间: 2023-11-26 08:04:21 浏览: 77
Android Build.VERSION.SDK_INT
这段代码有一些问题,主要是方法中的参数名和后面的变量名不一致,导致编译错误。以下是修改后的代码:
```
import java.awt.Point;
public class Box {
int x1, y1, x2, y2 = 0;
public Box buildBox(int x1, int y1, int x2, int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
return this;
}
public Box buildBox(Point topLeft, Point bottomRight) {
x1 = topLeft.x;
y1 = topLeft.y;
x2 = bottomRight.x;
y2 = bottomRight.y;
return this;
}
public Box buildBox(Point topLeft, int w, int h) {
x1 = topLeft.x;
y1 = topLeft.y;
x2 = (x1 + w);
y2 = (y1 + h);
return this;
}
public void printBox() {
System.out.println("Box:<" + x1 + "," + y1);
System.out.println("," + x2 + "," + y2 + " > ");
}
public static void main(String[] args) {
Box rect = new Box();
rect.buildBox(25, 25, 50, 50);
rect.printBox();
rect.buildBox(new Point(10, 10), new Point(20, 20));
rect.printBox();
rect.buildBox(new Point(10, 10), 50, 50);
rect.printBox();
}
}
```
在 main 方法中,我们需要先创建一个 Box 的实例对象才可以调用其它方法。另外,建议将类名的首字母大写以符合 Java 命名规范。
阅读全文