Java - 转换"this [int x,int y]"

Tra*_*ore 1 java this

我正在做的是将ac #project转换为java,以便在编写自定义类时进行练习.不幸的是,我无法弄清楚这个人在构造函数中使用"this"关键字做了什么.

// C# Code - How is this written in Java?
public Player this[int x, int y] 
{ 
    get { return squares[x, y]; } 
    set { squares[x, y] = value; } 
}
Run Code Online (Sandbox Code Playgroud)

我已经转换了很多代码,并且有点坚持这个.我似乎无法在Java中找到有关此特定实例的任何信息.有没有人对我有好的领导?

Jon*_*eet 9

那不是构造函数.这是索引器的声明.Java等价物将是这样的:

public Player getPlayer(int x, int y)
{
    // Note: Java doesn't have "real" multidimensional arrays,
    // only arrays of arrays.
    return squares[x][y];
}

public Player setPlayer(int x, int y, Player player)
{
    squares[x][y] = player;
}
Run Code Online (Sandbox Code Playgroud)