如何将带有数组值的私有二维枚举传递给另一个 class?
How to pass a private 2d enum with array values to another class?
我已经创建了一个地图变量,它是一个二维枚举(某些具有常量类型的图块),本质上我正在尝试制作它以便我可以通过多种方式渲染地图而无需将特定的渲染方法添加到地图中class,却变成了另一个class。
我现在有一个简单的控制台打印命令,但是当我使用嵌套循环遍历坐标时,我无法遍历数组值(打印),但我可以使用方法(getHeight( )、getWidth 等来自地图 class),因为它们是 public,而不是数组值,因为地图在 TiledMap class 中是私有的。
我尝试过使用 'this',但我认为我没有正确使用它,我不明白如何使用 'this' 传递地图数组,我已经检查过了在线查看 'this' 的示例,但我还找不到任何解决方案。我假设因为我将地图传递给渲染器,所以我也可以访问数组值,但我不能。
问题:"The type of the expression must be an array type but it resolved to TiledMap"
public class ConsoleRenderer implements MapRenderer{
@Override
public void render(TiledMap map) {
//map = TiledMap(this); < attempt at using 'this'
for (int row = 0; row < map.getWidth(); row++) {
for (int col = 0; col < map.getHeight(); col++) {
System.out.print(map[col][row].asChar()); //Problem occurs<<<
}
System.out.println(" ");
}
}
}
Final class TiledMap implements ITiledMap{
private TerrainType[][] mMap;
private MapRenderer mRenderer;
public TiledMap(int aWidth, int aHeight, TerrainType aType, ConsoleRenderer Renderer)
{
mMap = new TerrainType [aWidth][aHeight];
for (int x=0; x<aWidth; x++)
{
for (int y=0; y<aHeight; y++)
{
mMap[x][y] = aType;
}
}
this.mMap = mMap;
//sets the method of renderer
setRenderer(Renderer);
}
任何相关的 material 或指点将不胜感激,谢谢。
在 tiledMap class 中你不必使用 this.mMap = mMap
因为你还没有在 constructor.You 中创建局部变量只是让 mMap 等于 itself.You 在 for loop.You 中将值传递给它,只有当你有一个与全局变量同名的局部变量时,才应该使用 this
。
在您的 ConsoleRenderer class 中,您传递的是 class TiledMap 而不是 array.You 必须具有 getter 才能访问您的数组 mMap
。但是当您传递TiledMap named map 你把它当作一个数组来使用你不能像数组一样使用它,因为它只是一个 class 而不是 classes 的数组。
像这样为你的数组 mMap 创建一个 getter 函数:
public TerrainType[][] getTerrainType(){
return mMap;
}
然后在 consoleRenderer 中你可以这样做:
map.getTerrainType[][]
我已经创建了一个地图变量,它是一个二维枚举(某些具有常量类型的图块),本质上我正在尝试制作它以便我可以通过多种方式渲染地图而无需将特定的渲染方法添加到地图中class,却变成了另一个class。
我现在有一个简单的控制台打印命令,但是当我使用嵌套循环遍历坐标时,我无法遍历数组值(打印),但我可以使用方法(getHeight( )、getWidth 等来自地图 class),因为它们是 public,而不是数组值,因为地图在 TiledMap class 中是私有的。
我尝试过使用 'this',但我认为我没有正确使用它,我不明白如何使用 'this' 传递地图数组,我已经检查过了在线查看 'this' 的示例,但我还找不到任何解决方案。我假设因为我将地图传递给渲染器,所以我也可以访问数组值,但我不能。
问题:"The type of the expression must be an array type but it resolved to TiledMap"
public class ConsoleRenderer implements MapRenderer{
@Override
public void render(TiledMap map) {
//map = TiledMap(this); < attempt at using 'this'
for (int row = 0; row < map.getWidth(); row++) {
for (int col = 0; col < map.getHeight(); col++) {
System.out.print(map[col][row].asChar()); //Problem occurs<<<
}
System.out.println(" ");
}
}
}
Final class TiledMap implements ITiledMap{
private TerrainType[][] mMap;
private MapRenderer mRenderer;
public TiledMap(int aWidth, int aHeight, TerrainType aType, ConsoleRenderer Renderer)
{
mMap = new TerrainType [aWidth][aHeight];
for (int x=0; x<aWidth; x++)
{
for (int y=0; y<aHeight; y++)
{
mMap[x][y] = aType;
}
}
this.mMap = mMap;
//sets the method of renderer
setRenderer(Renderer);
}
任何相关的 material 或指点将不胜感激,谢谢。
在 tiledMap class 中你不必使用 this.mMap = mMap
因为你还没有在 constructor.You 中创建局部变量只是让 mMap 等于 itself.You 在 for loop.You 中将值传递给它,只有当你有一个与全局变量同名的局部变量时,才应该使用 this
。
在您的 ConsoleRenderer class 中,您传递的是 class TiledMap 而不是 array.You 必须具有 getter 才能访问您的数组 mMap
。但是当您传递TiledMap named map 你把它当作一个数组来使用你不能像数组一样使用它,因为它只是一个 class 而不是 classes 的数组。
像这样为你的数组 mMap 创建一个 getter 函数:
public TerrainType[][] getTerrainType(){
return mMap;
}
然后在 consoleRenderer 中你可以这样做:
map.getTerrainType[][]