public class Location {
final public static int INITIAL = 0;
final public static int PRISON = 1;
final public static int DEATH = 2;
final public static int SQUARE = 3;
private String name;
private int type;
private int section;
private int damage;
private Square square ;
// this constructor construct a Location from a name and a type.
public Location(String name, int type) {
this.name = name;
this.type = type;
}
// this constructor constructs a Location of type SQUARE from a name, section, and damage.
public Location(String name, int section, int damage) {
this.name = name;
this.section = section;
this.damage = damage;
this.square = new Square(name,section,damage);
}
// Get the square associated with this Location.
public Square getSquare() {
return square;
}
}
我想我误解了第二个构造函数正在做什么,因为目前构造函数没有对实例变量square做任何事情。如何使用构造函数在Java中初始化其他类的对象?
第二个构造函数的注释声明它创建了SQUARE类型的位置,但代码未能将'type'字段设置为SQUARE。 – FredK