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

时间:2011-08-13 09:10:28

标签: java inheritance constructor compiler-errors

我正在编写一个继承自另一个类(HotelRoom)的类(Suite)。 HotelRoom类有一个构造函数,需要一个参数(一个int),因此在Suite I的构造函数中称为super(room),我可以告诉它应该工作。 HotelRoom符合标准,但Suite给出了构造函数错误。任何帮助将不胜感激。以下是我的代码:

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);
    }

}

和我的子类:

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);
    }

}

确切的编译错误:

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

我已经保存并重新编译了几次,我得到了相同的结果。 HotelRoom编辑很好,但套房没有。它们是目录中唯一的两个java文件,因此调用错误的类没有问题。 :)

1 个答案:

答案 0 :(得分:0)

我认为它与您在Mac上的Java设置有关。在运行Linux的PC上使用Java 1.6,它编译并运行正常,使用以下Main类进行测试

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

输出:

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