如何从另一个类中为该类的对象调用accsessor方法?

时间:2016-11-19 19:30:57

标签: java arrays object

我正在尝试生成一个程序生成的2D地图,但我仍然坚持创建accsessor方法来返回特定位置的类型。 我创建了一个自定义方形类,它具有变量类型和特殊..

这是方形类

public class Square{//custom object for map class
private int type = 0;
private boolean special = false;

public int getType(){//returns type of square
  return type;
}

public boolean isSpecial(){//returns boolean for whether square is special or not
 return special;
}

public void setType(int a){//sets type for square if input is between 1 and 5
if(a>=1&&a<=5){
    type = a;
   }
}

public void setSpecial(){//sets special status to true
special = true;
}
}

我测试了方形类以确保它有效,当我尝试创建一个在给定坐标处返回方形类型的方法时,我在Map类中遇到麻烦

这是该类的适用代码

public class Map{
private int mapWidth;
private int mapHeight;
Square newMap[][] = new Square[0][0];

public Map(int width, int height){//constructor sets map height and width and instantiates a 2d array with the arguments
  if(width <= 0){//if user inputs numbers less than 1 will set according attribute to 1
    width = 1;
  }
  if (height <= 0){
    height = 1;
  }
  mapWidth = width;
    mapHeight = height;
  Square newMap[][] = new Square[mapWidth][mapHeight];
}

public void create(){



}

public int getWidth(){//accssesor methods
  return mapWidth;
}

public int getHeight(){
  return mapHeight;
}

public int getType(int x, int y){
  return newMap[y][x].getType();
}

}

我知道我可能正在做一些可怕的错误,但是我们将非常感谢任何帮助。

0 个答案:

没有答案