我创建了两个类StandardFamily,它们扩展了抽象类Room,在编译Standard类时遇到错误“无法在调用超类型构造函数之前引用roomNumber”的错误,但是我不明白为什么,任何帮助,将不胜感激。

Room

public abstract class Room
{
    public int roomNumber;
    public String roomType;
    public boolean ensuite;
    public boolean available;

public Room(int roomNumber, boolean enSuite)
   {
       this.roomNumber = roomNumber;
       ensuite = enSuite;
       available = false;
   }
}


Standard

public class Standard extends Room
{
    private int roomNumber;
    private int childCap;
    private int adultCap;



public Standard(int theNumber, int kidsCap, int adultsCap)
   {
       super(theNumber, roomNumber);
       childCap = childsCap;
       adultCap = AdultsCap;
   }
}

最佳答案

super(theNumber, roomNumber);

调用超级构造函数时,它必须是您要做的第一件事。您正在尝试将私有字段roomNumber作为参数发送。

另外,当您发送两个int时,Room构造函数接受一个int和一个布尔值。

public Room(int roomNumber, boolean enSuite)

09-28 06:30