在调用超类型构造函数之前无法引用 roomNumber

发布于 2024-12-16 18:09:07 字数 849 浏览 0 评论 0原文

我创建了两个类 StandardFamily ,它们扩展了抽象类 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

莳間冲淡了誓言ζ 2024-12-23 18:09:07

super(theNumber, roomNumber);

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

附带说明一下,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)

提赋 2024-12-23 18:09:07
  super(theNumber, roomNumber);//both are int

当你这样写的时候,编译器会寻找一个有两个int参数的超级构造函数,但是并没有这样的构造函数。您的构造函数有一个 int 和一个 Boolean

public Room(int roomNumber, boolean enSuite)

在 Java 中如果父构造函数由于任何条件而无法进行调用,则编译器应将其显示为错误。

来自 JLS-8.8.7.1

构造函数体中的显式构造函数调用语句不能引用此类或任何超类中声明的任何实例变量、实例方法或内部类,也不能在任何表达式中使用 this 或 super;否则,会出现编译时错误。
因此,更改构造函数参数或创建一个新参数...

  super(theNumber, roomNumber);//both are int

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 one int and one Boolean.

public Room(int roomNumber, boolean enSuite)

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

An explicit constructor invocation statement in a constructor body may not refer to any instance variables or instance methods or inner classes declared in this class or any superclass, or use this or super in any expression; otherwise, a compile-time error occurs.
So change your constructor parameter or make a new one...

呆橘 2024-12-23 18:09:07

您正在引用标准中的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文