Java point类move函数
时间: 2023-06-12 18:04:37 浏览: 76
假设你有一个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类
下面是一个 Java 中的 Point 类的示例:
```java
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 void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public void setLocation(int x, int y) {
this.x = x;
this.y = y;
}
public void move(int dx, int dy) {
x += dx;
y += dy;
}
}
```
这个 Point 类表示一个二维平面中的点,有两个私有属性 x 和 y,分别表示横坐标和纵坐标。它有一个带参数的构造函数,用于初始化 x 和 y 属性。getX 和 getY 方法用于获取 x 和 y 属性的值,setX 和 setY 方法用于设置 x 和 y 属性的值,setLocation 方法用于设置 x 和 y 属性的值。move 方法用于将点沿 x 和 y 轴移动一定的距离。
java矩形类继承闭合图形抽象类的Figure和point1方法,实现其中成员方法
以下是Java矩形类的示例代码:
```
public class Rectangle extends Figure {
private double width;
private double height;
public Rectangle(double x, double y, double width, double height) {
super(new Point(x, y));
this.width = width;
this.height = height;
}
public double getWidth() {
return width;
}
public double getHeight() {
return height;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width + height);
}
public boolean contains(Point p) {
return (p.getX() >= getPoint().getX() && p.getX() <= getPoint().getX() + width &&
p.getY() >= getPoint().getY() && p.getY() <= getPoint().getY() + height);
}
public void move(double dx, double dy) {
Point newPoint = new Point(getPoint().getX() + dx, getPoint().getY() + dy);
setPoint(newPoint);
}
}
```
在这个示例中,我们定义了一个名为Rectangle的类,它继承了Figure抽象类,并实现了其中的成员方法。我们在构造函数中传入矩形的起点坐标和宽度、高度参数,并通过调用父类的构造函数来初始化Figure对象。
在Rectangle类中,我们还定义了getWidth()和getHeight()方法来获取矩形的宽度和高度,以及getArea()和getPerimeter()方法来计算矩形的面积和周长。我们还实现了contains()方法,用于判断一个点是否在矩形内部,以及move()方法,用于移动矩形的起点坐标。
需要注意的是,我们在Rectangle类中调用了父类Figure的setPoint()和getPoint()方法,这是因为Figure类中的成员变量point是私有的,无法直接访问。因此,我们需要通过调用父类的方法来操作point变量。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)