在调用超类型构造函数之前无法引用 roomNumber
我创建了两个类 Standard
和 Family
,它们扩展了抽象类 Room
,当编译我遇到的 Standard
类时出现错误“在调用超类型构造函数之前无法引用 roomNumber”,但我不明白为什么,任何帮助将不胜感激。
房间
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;
}
}
标准
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;
}
}
i have created two classes Standard
and Family
which extend abstract class Room
, when compiling the Standard
class i am met with the error "cannot reference roomNumber before supertype constructor has been called" but i cannot understand why, any help would be appreciated.
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
super(theNumber, roomNumber);
当调用超级构造函数时,它必须是您要做的第一件事。您正在尝试将私有字段 roomNumber 作为参数发送。
附带说明一下,Room 构造函数接受一个 int 和一个布尔值,而您发送两个 int 值。
公共房间(int roomNumber, boolean enSuite)
super(theNumber, roomNumber);
when calling a super constructor it must be the first thing you do. you're trying to send the private field roomNumber as a parameter.
on a side note, the Room constructor takes an int and a boolean value, while you are sending two ints.
public Room(int roomNumber, boolean enSuite)
当你这样写的时候,编译器会寻找一个有两个
int参数
的超级构造函数,但是并没有这样的构造函数。您的构造函数有一个int
和一个Boolean
。在 Java 中如果父构造函数由于任何条件而无法进行调用,则编译器应将其显示为错误。
来自 JLS-8.8.7.1
When you write this, the compiler will search for a super constructor that has two
int parameter
, but there is no such constructor. Your constructor has oneint
and oneBoolean
.In Java if the parent constructor can't make the call due to any condition, then the compiler should show it as an error.
From JLS-8.8.7.1
您正在引用标准中的
roomNumber
,它仅在 Room() 的构造函数完成处理后才由 JVM 处理。仅当所有超类的构造函数成功返回后,类级别变量才会由 JVM 进行处理和初始化。所以你的调用链是这样的:Standard() ->房间() -> [初始化Room的成员变量]->从 Room() 返回 -> [初始化 Standard 的成员变量]
因此,您不能将 roomNumber 传递给 Room,因为实际上它还不存在。
you are referencing
roomNumber
in Standard, which is only processed by the JVM once the constructor of Room() has finished processing. Class level variables are only processed and initialised by the JVM once the constructors of all superclasses have returned successfully. So your call chain is something like:Standard() -> Room() -> [ initialise Room's member variables] -> Return from Room() -> [initialise Standard's member variables]
As such, you cannot pass roomNumber up to Room, because in effect it will not yet exist.