用java 定义Point类描述屏幕上的一个点。该 类包括点的x和y两个坐标,及对点进行的相 关操作,如:取得点的坐标值,对点的坐标进 行赋值。此类属于MyPackageTwoD包。此包 中另一个Rectangle类可以实现矩形的基本操 作,包含Point. width height等了个变量 及构造方法和其他方法。文件 TestPackage.java中包含maino方法,定义如 T:
时间: 2024-01-21 16:20:18 浏览: 53
以下是Point类的定义:
```
package MyPackageTwoD;
public class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
}
```
以下是Rectangle类的定义:
```
package MyPackageTwoD;
public class Rectangle {
private Point topLeft;
private int width;
private int height;
public Rectangle(Point topLeft, int width, int height) {
this.topLeft = topLeft;
this.width = width;
this.height = height;
}
public Point getTopLeft() {
return topLeft;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public void setTopLeft(Point topLeft) {
this.topLeft = topLeft;
}
public void setWidth(int width) {
this.width = width;
}
public void setHeight(int height) {
this.height = height;
}
}
```
以下是TestPackage.java中的main方法:
```
package MyPackageTwoD;
public class TestPackage {
public static void main(String[] args) {
Point p = new Point(3, 4);
System.out.println("x = " + p.getX() + ", y = " + p.getY());
Rectangle r = new Rectangle(p, 5, 6);
System.out.println("width = " + r.getWidth() + ", height = " + r.getHeight());
}
}
```
阅读全文