我正在写一个从另一个类(HotelRoom)继承的类(Suite)。 HotelRoom类具有一个构造函数,该构造函数需要一个参数(一个int值),因此在Suite的构造函数中,我将其称为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:找不到符号
符号:构造函数HotelRoom(int)
位置:class HotelRoom
super (房间);
^
1个错误
我已经保存并重新编译了几次,但我得到的结果相同。 HotelRoom可以编译,但Suite不能。它们是目录中仅有的两个Java文件,因此调用错误的类没有问题。 :)
最佳答案
我认为这与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