这是代码
public class Main {
public static void main(String[] args) throws InterruptedException {
// A running ball
class Ball {
private double x, y, radius, xvel, yvel;
private long timeframe;
private boolean moving;
public Ball(double x, double y, double radius, double xvel, double yvel, long timeframe) throws InterruptedException {
super();
this.x = x;
this.y = y;
this.radius = radius;
this.xvel = xvel;
this.yvel = yvel;
this.timeframe = timeframe;
this.moving = true;
while(moving) {
// The next print line is what i want to run outside of this constructor
System.out.println("x:" + getX() + " " + "y:" + getY());
move();
Thread.sleep(timeframe);
}
}
private void move() {
x += xvel;
y += yvel;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public void setXvel(double xvel) {
this.xvel = xvel;
}
public void setYvel(double yvel) {
this.yvel = yvel;
}
}
Ball ball = new Ball(0, 0, 1, .5, .5, 1000);
}
}现在,我想在ball运行时访问它的x和y字段。我不能在这里做ball.getX(),因为构造函数中有一个循环要先完成。当循环运行时,我如何做到这一点。我有使用线程的想法,但我看不到实现细节。
发布于 2018-01-10 03:34:38
晚上好Mac JalLoh
我认为您正在搜索的是同步的Java关键字:https://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html以及Java7中引入的Executor框架:https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executor.html
简而言之,如果你想让球移动,如果外部观察者必须读取轨迹,你可能需要两个不同的线程,这两个线程在访问同一个对象时是同步的。事实是,在Java中,您不能同时修改球的位置和读取它。为什么?因为你永远不能确定职位何时被更新。这会导致位置读取和写入的不确定性(请查看google中的线程安全)。我们不喜欢不确定性,所以我们放了synchronized关键字(你可以说它是先到先服务的)。
此外,在我看来,在ball构造函数中包含球的移动也不是一个好的做法。事实上,构造器回答了“什么是球(具有原点和半径的3D对象)”的问题,而平移或旋转运动回答了运动的物理问题。这两个问题需要解除关联(您可以引入一个作用于Ball实例的外部对象)
发布于 2018-01-10 13:08:10
我想我终于找到了解决我needed.There的代码的方法。
public class Main {
// a running ball
public class Ball extends Circle implements Runnable{
private double xvel, yvel;
private long timeframe;
private boolean moving = true;
public boolean isMoving() {
return moving;
}
public void setMoving(boolean moving) {
this.moving = moving;
}
public Ball(double x, double y, double radius, double xvel, double yvel, long timeframe) {
super(new Point(x, y), radius);
this.xvel = xvel;
this.yvel = yvel;
this.timeframe = timeframe;
new Thread(this).start();
}
@Override
public void run() {
while(moving) {
move(xvel, yvel);
try {
Thread.sleep(timeframe);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public double getX() {
return getCenter().getX();
}
public double getY() {
return getCenter().getY();
}
public void setXvel(double xvel) {
this.xvel = xvel;
}
public void setYvel(double yvel) {
this.yvel = yvel;
}
}
public static void main(String[] args) throws InterruptedException {
// a Ball fields getter
class Getter implements Runnable{
Ball ball;
public Getter(Ball ball) {
this.ball = ball;
}
public void run() {
while(ball.isMoving()) {
System.out.println(ball.getX() + " " + ball.getY());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Ball ball = new Main().new Ball(0, 0, 1, .5, .5, 1000);
Thread getter = new Thread(new Getter(ball));
getter.start();
}
}我喜欢将移动机制放在构造器中,因为它看起来更自然,因为我正在建模的是一个移动的球,它不需要外部参与者来使其移动!
https://stackoverflow.com/questions/48175121
复制相似问题