我正在開發一個小項目。將有一個主要抽象類的預訂,兩個子類,一個用於酒店預訂,另一個用於航班預訂,它們都繼承抽象類的方法,例如用戶的姓名/地址等。使用SuperClass/Inheritance /構造函數問題
嘗試爲一個新的酒店預訂創建一個構造函數,我遇到了一個問題,它使用super()函數繼承了抽象類中的方法/變量。
的錯誤:
Constructor in class booking can not be applied to given types.
這裏是我的代碼:
import java.util.Scanner;
abstract class Booking{
private String fname;
private String lname;
private int HouseNo;
private String Street;
private String Postcode;
Scanner in = new Scanner(System.in);
//Parameterized Constructor
Booking(String FirstName, String Lastname, int Housenumber, String thestreet, String
thePostcode)
{
fname = FirstName;
lname = Lastname;
HouseNo = Housenumber;
Street = thestreet;
thePostcode = Postcode;
}
//Acessor Methods below
String getfname()
{
fname = "sds";
return fname;
}
void setFname(String FirstName)
{
fname = FirstName;
}
String getlname()
{
return lname;
}
void setLname(String LastName)
{
lname = LastName;
}
int getHouseNo()
{
return HouseNo;
}
void setHouseNo(int HouseNumber)
{
HouseNo = HouseNumber;
}
String getStreet()
{
return Street;
}
void setStreet(String StreetName)
{
Street = StreetName;
}
String getPostcode()
{
return Postcode;
}
void setPostcode(String ThePostcode)
{
Postcode = ThePostcode;
}
abstract public String Verification();
{
}
import java.util.Scanner;
class Hotel extends Booking{
Scanner in = new Scanner(System.in);
private String TheVerification;
private int guests;
private String Roomtype;
Hotel(){
super();
TheVerification = Verification();
guests = 0;
Roomtype = Roomtype();
}
public String Verification(){
System.out.println("Please provide your car reg as verification");
TheVerification = in.next();
return TheVerification;
}
public String Roomtype(){
System.out.println("Would you like a Premiun or Regular room?");
Roomtype = in.next();
return Roomtype;
}
public void print(){
System.out.println("Roomtype" + Roomtype);
}
}
那麼是什麼問題? – Adrian 2012-02-28 14:10:40
您不必特別調用超類的構造函數。你沒有嘗試過嗎? – tartak 2012-02-28 14:11:51
我想要Sub類的構造函數使用超類方法..或者我吠叫錯誤的樹。基本上在程序交互中,用戶將使用Hotel側的構造函數,輸入所有需要的元素。對於我來說抽象類的想法是防止事情被寫出兩次,並且使用一個是項目的一個要求。 (本質上創建一個包括X,Y,Z的程序)如果我解決了這個錯誤的方式,請這麼說,只能從錯誤中學習.. – 2012-02-28 14:17:19