简体   繁体   English

Java 找不到符号:构造函数错误

[英]Java cannot find symbol: constructor error

I am writing a class (Suite) that is inheriting from another class (HotelRoom).我正在编写一个继承自另一个 class (HotelRoom) 的 class (Suite)。 The HotelRoom class has a constructor that requires an argument (an int) and so in the constructor for Suite I called super(room) which from what I can tell should work. HotelRoom class 有一个需要参数(int)的构造函数,因此在 Suite 的构造函数中,我称之为 super(room),据我所知应该可以工作。 HotelRoom complies just fine, however Suite gives the constructor error. HotelRoom 符合要求,但 Suite 给出了构造函数错误。 Any help would be greatly appreciated.任何帮助将不胜感激。 Here is my code below:下面是我的代码:

public class HotelRoom
{
    private int roomNumber;
    protected double nightlyRate;
    private final int maxRoomNumber = 999;
    boolean didEnterCorrectRoomNumber = false;

    public HotelRoom(int room)
    {

        if (room > 0 && room <= 299)
        {
            nightlyRate = 69.95;
            didEnterCorrectRoomNumber = true;
            //return didEnterCorrectRoomNumber;
        }
        else if (room > 299 && room <= maxRoomNumber)
        {
            nightlyRate = 89.95;
            didEnterCorrectRoomNumber = true;
            //return didEnterCorrectRoomNumber;
        }
        else
        {
            //return didEnterCorrectRoomNumber;
        }
    }

    public int getRoomNumber ()
    {
        return roomNumber;
    }

    public double getNightlyRate ()
    {
        return nightlyRate;
    }
    public boolean getDidEnterCorrectRoomNumber ()
    {
        return didEnterCorrectRoomNumber;
    }

    public void displayRoom ()
    {
        System.out.println("Room Number: " + roomNumber);
        System.out.format("Cost per Night: $%.2f%n", nightlyRate);
    }

}

and my subclass:和我的子类:

public class Suite extends HotelRoom
{
    private final double suiteSurchargeRate = 40.00;
    private double nightlyRateWithSuite;

    public Suite (int room)
    {
        super(room);
        //boolean didEnterCorrectRoomNumber = super.getDidEnterCorrectRoomNumber();
        nightlyRateWithSuite = super.getNightlyRate() + suiteSurchargeRate;
        //return didEnterCorrectRoomNumber;
    }

    public void displayRoom ()
    {
        super.displayRoom();
        System.out.format("Suite Surcharge: $%.2f%n", suiteSurchargeRate);
        System.out.format("Total Cost per Night: $%.2f%n", nightlyRateWithSuite);
    }

}

Exact compiler error:确切的编译器错误:

MacBook-Air:HotelRoom Nick$ javac Suite.java
Suite.java:12: cannot find symbol
symbol  : constructor HotelRoom(int)
location: class HotelRoom
    super(room);
    ^
1 error

I have saved and recompiled both a few times and I just get the same result.我已经保存并重新编译了几次,我得到了相同的结果。 HotelRoom compiles fine, but Suite does not. HotelRoom 可以正常编译,但 Suite 不能。 They are the only two java files in their directory, so there are no issues with calling the wrong class.它们是其目录中仅有的两个 java 文件,因此调用错误的 class 没有问题。 :) :)

I think it relates to your Java setup on your Mac.我认为这与您在 Mac 上的 Java 设置有关。 Using Java 1.6 on a PC running Linux it compiles and runs fine for me, tested with following Main class在运行 Linux 的 PC 上使用 Java 1.6 它可以编译并运行良好,并使用以下 Main class 进行测试

public class Main {
    public static void main(String[] args) {        
        Suite suite = new Suite(10);
        suite.displayRoom();    
    }
}

Output: Output:

Room Number: 0
Cost per Night: $69.95
Suite Surcharge: $40.00
Total Cost per Night: $109.95

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM