在Java中如何创建(实际上是什么)指针

huangapple go评论79阅读模式
英文:

How to make (what is effectively) a pointer in Java

问题

我知道Java中没有指针类型,但我有一个对象数组,我需要类似指向数组第一个元素的指针的东西,无论它是什么,或者指向的对象改变了多少次。这是我正在开发的一个宠物小精灵游戏,我在Player类中有以下代码:

public class Player {

    public String name;
    public Pokemon[] party;
    public static Pokemon currentPokemon;

    public Player(){
        this.party = new Pokemon[6];
        currentPokemon = party[0];
    }
}

我需要currentPokemon始终“指向”party[0],这就是我认为要将其设置为静态的原因,但当我稍后向数组添加宠物时,party[0]包含了宠物,但currentPokemon却为null。我如何获得这种类似指针的行为?

英文:

I know that there are no pointer types in Java, but I have an array of objects, and I need something to work like a pointer to the first element of the array not matter what it is or how many times the pointee changes. This is for a Pokemon game I am working on, and in the Player class, I have

public class Player {

    public String name;
    public Pokemon[] party;
    public static Pokemon currentPokemon;

    public Player(){
        this.party = new Pokemon[6];
        currentPokemon = party[0];
    }
}

I need currentPokemon to always "point" to party[0], which is why I thought to make it static, but when I later add pokemon to the array, party[0] cointains the Pokemon but currentPokemon is null. How do I get this pointer-like behavior?

答案1

得分: 3

static 成员属于 class,并且被所有实例共享。每次创建新实例时,都会覆盖 currentPokemon 的值(在构造函数中)。移除 static 修饰符,问题应该就解决了:

public class Player {

    public String name;
    public Pokemon[] party;
    public Pokemon currentPokemon; // 这里

编辑:
根据 Christian Cook 最后的评论,我现在认为我理解了问题陈述。currentPokemon 在构造函数中使用 party[0] 进行了初始化,而当然此时 party[0]null,因为 party 刚刚在前一行创建。与 C(++)中的指针不同,currentPokemon 不会在 party 更新时更新 - 它不是指针,只是一个对 party[0] 进行初始化时的值的引用。

获得你所寻求的行为最简单的方法是不将 currentPokemon 存储为成员,而是创建一个检索它的方法:

public Pokemon getCurrentPokemon() {
    return party[0];
}
英文:

static members belong to the class, and are shared by all instances. Every time you create a new instance, you override currentPokemon's value (in the constructor). Remove the static modifier and you should be OK:

public class Player {

    public String name;
    public Pokemon[] party;
    public Pokemon currentPokemon; // Here

EDIT:<br/>
With the last comment by Christian Cook I think I now understand the problem statement. currentPokemon is initialized in the constructor with party[0], which is of course null, because party was just created on the previous line. Unlike a pointer in c(++), currentPokemon won't update when party does - it isn't a pointer, it's just a reference to the value party[0] had when it was initialized.

The easiest way to get the behavior you're looking for is to not store currentPokemon as a member, but has a method to retrieve it instead:

public Pokemon getCurrentPokemon() {
    return party[0];
}

答案2

得分: -1

你可以这样引用数组

Pokemon[] currentPokemon = party

现在,party 和 currentPokemon 都指向同一个对象数组。操作 currentPokemon 会导致操作 party。

希望这能有些帮助。 在Java中如何创建(实际上是什么)指针

英文:

You can point to arrays like this

Pokemon[] currentPokemon=party

Now, party and currentPokemon are pointing to the same array of objects. Manipulating currentPokemon results in manipulating party.

Hope this helps a little. 在Java中如何创建(实际上是什么)指针

huangapple
  • 本文由 发表于 2020年3月16日 12:53:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/60700491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定