java point类
时间: 2023-10-02 19:11:44 浏览: 114
Point类是Java中一个表示二维坐标点的类,它包含两个int类型的成员变量x和y,分别表示这个点的横坐标和纵坐标。
Point类提供了以下方法:
1. 构造方法:
- Point():创建一个坐标为(0,0)的点。
- Point(int x, int y):创建一个坐标为(x,y)的点。
- Point(Point p):创建一个与p点坐标相同的点。
2. 成员方法:
- int getX():返回该点的横坐标。
- int getY():返回该点的纵坐标。
- setLocation(int x, int y):设置该点的坐标为(x,y)。
- setLocation(Point p):设置该点的坐标与p点相同。
- move(int x, int y):将该点的坐标移动x个单位横坐标、y个单位纵坐标。
- translate(int dx, int dy):将该点的坐标增加dx个单位横坐标、dy个单位纵坐标。
Point类是一个非常简单的类,但它在Java的GUI编程中经常用到,比如用来表示鼠标的位置、图形的顶点等。
相关问题
Java point类move函数
假设你有一个Java类Point,代表一个点的二维坐标(x,y),下面是一个move函数,用于移动该点的位置:
```java
public class Point {
private double x;
private double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public void move(double deltaX, double deltaY) {
x += deltaX;
y += deltaY;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
}
```
这个函数接受两个参数deltaX和deltaY,分别代表在x和y方向上需要移动的距离。它会将点的x和y坐标分别加上这两个值,实现移动操作。
用JAVA声明point类
可以使用以下代码声明一个point类:
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;
}
}