如何在 java 中使用 getY() 方法?
How to use a getY() method in java?
我在这个网站上发现了很多关于如何在 java 中使用 getter 函数的问题,但我还没有找到大多数这些问题的真正答案,这就是我问的原因这个。
这是我的一段代码:
y - 形状的高度
x - 形状宽度
posX - 形状在 x 轴上的位置
posY - 形状在 y 轴上的位置
speed - 行驶速度
speedY - 等于速度,专门用于y轴运动
halfY - y 和距离形状的一半应该在转身前向下移动
public void actionPerformed(ActionEvent e){
if(posY == 0){
posX = posX + speed;
}
if((posX <= 0)&&(posY != 0)){
posX = posX + speed - speed;
posY = posY + speedY;
}
if(posX >= 600 - x){
posX = posX + speed - speed;
posY = posY + speedY;
}
if(posY >= y - halfY){
posX = posX - speed;
}
repaint();
}
移动后我想获取形状的当前 y 位置并在另一种方法中使用它,但我不确定如何执行此操作,这也是一个 void 函数所以我不确定如何获取 y 和在同一个方法中使用它 CLASS.
这里有一个基本的 getter 函数供您使用(假设您的 posX/Y 是积分)
public Point getX() {
return this.posX;
}
public Point getY() {
return this.posY;
}
如果你想在另一个 class 中使用这个 class 的实例调用它,那么只需执行 objectname.getX/Y();
此外,如果这些位置是实例数据(它们应该是),您可以调用 objectname。posX/Y;,尽管除非必要,否则我更喜欢使用方法。
如果这不能回答您的问题,请包含完整的 class 以及对您尝试执行的操作的更详细描述。
希望我有所帮助。
假设您有一个类似于以下内容的 class
public class Point {
// This fields can't be accessed outside of this class
private int x;
private int y;
/*....More code...*/
// To be able to update and acces the fields x and y from the outside
// you need getters and setters.
// The getters
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}
// The setters
public void setX(int new_x){
this.x = new_x;
}
public void setY(int new_y){
this.y = new_y;
}
}
您更新后的代码现在看起来像这样。
public void actionPerformed(ActionEvent e){
if(pos.getX() == 0){
pos.setX(pos.getX() + speed);
}
else if((pos.getX() <= 0) && (pos.getY() != 0)){
pos.setX(pos.getX() + speed - speed);
pos.setY(pos.getY() + speedY);
}
else if(pos.getX() >= 600 - x){
pos.setX(pos.getX() + speed - speed);
pos.setY(pos.getY() + speedY);
}
else if(pos.getY() >= y - halfY){
pos.getX(pos.getX() - speed);
}
repaint();
}
其中 pos
是 Point
的实例。
我在这个网站上发现了很多关于如何在 java 中使用 getter 函数的问题,但我还没有找到大多数这些问题的真正答案,这就是我问的原因这个。
这是我的一段代码:
y - 形状的高度
x - 形状宽度
posX - 形状在 x 轴上的位置
posY - 形状在 y 轴上的位置
speed - 行驶速度
speedY - 等于速度,专门用于y轴运动
halfY - y 和距离形状的一半应该在转身前向下移动
public void actionPerformed(ActionEvent e){
if(posY == 0){
posX = posX + speed;
}
if((posX <= 0)&&(posY != 0)){
posX = posX + speed - speed;
posY = posY + speedY;
}
if(posX >= 600 - x){
posX = posX + speed - speed;
posY = posY + speedY;
}
if(posY >= y - halfY){
posX = posX - speed;
}
repaint();
}
移动后我想获取形状的当前 y 位置并在另一种方法中使用它,但我不确定如何执行此操作,这也是一个 void 函数所以我不确定如何获取 y 和在同一个方法中使用它 CLASS.
这里有一个基本的 getter 函数供您使用(假设您的 posX/Y 是积分)
public Point getX() {
return this.posX;
}
public Point getY() {
return this.posY;
}
如果你想在另一个 class 中使用这个 class 的实例调用它,那么只需执行 objectname.getX/Y();
此外,如果这些位置是实例数据(它们应该是),您可以调用 objectname。posX/Y;,尽管除非必要,否则我更喜欢使用方法。
如果这不能回答您的问题,请包含完整的 class 以及对您尝试执行的操作的更详细描述。
希望我有所帮助。
假设您有一个类似于以下内容的 class
public class Point {
// This fields can't be accessed outside of this class
private int x;
private int y;
/*....More code...*/
// To be able to update and acces the fields x and y from the outside
// you need getters and setters.
// The getters
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}
// The setters
public void setX(int new_x){
this.x = new_x;
}
public void setY(int new_y){
this.y = new_y;
}
}
您更新后的代码现在看起来像这样。
public void actionPerformed(ActionEvent e){
if(pos.getX() == 0){
pos.setX(pos.getX() + speed);
}
else if((pos.getX() <= 0) && (pos.getY() != 0)){
pos.setX(pos.getX() + speed - speed);
pos.setY(pos.getY() + speedY);
}
else if(pos.getX() >= 600 - x){
pos.setX(pos.getX() + speed - speed);
pos.setY(pos.getY() + speedY);
}
else if(pos.getY() >= y - halfY){
pos.getX(pos.getX() - speed);
}
repaint();
}
其中 pos
是 Point
的实例。