我希望jar的移动位置与玩家移动的距离相同。
public class Player { // instance variables - replace the example below with your own private String name; private int position; private Jar jar;
public Player()
{
position = 0;
jar = new Jar();
System.out.print("Enter player's name: ");
this.name = Global.keyboard.nextLine();
}
public int move(int distance)
{
position = position + distance;
}
}
public class Jar { private int position; private Stone stone;
public Jar()
{
position = 0;
stone = null;
}
public Jar(int initPos, Stone stone)
{
position = initPos;
this.stone = stone;
}
public void move()
{
Player move = new Player();
}
}
你可以在Player内部修改jar的位置。而且,你可能不想每次换位置都创建一个新Player。
public class Player {
...
public int move(int distance) {
position = position + distance;
jar.move(distance);
}
}
public class Jar { private int position; private Stone stone;
public Jar() {
position = 0;
stone = null;
}
public Jar(int initPos, Stone stone) {
position = initPos;
this.stone = stone;
}
public void move(int distance) {
position += distance;
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。