在下面的代碼中,我提出了幾個問題: 我無法將類Address
的對象作爲參數傳遞給其子類的構造函數EmployeeAddress
。爲什麼這樣 ?它給錯誤,如參數不匹配..沒有參數傳遞.... 我想調用EmployeeAddress
中的Address
對象在show方法。怎麼做?如何將父對象作爲參數傳遞給子類的構造函數
class Address {
public String street;
int pin;
String city;
Address(String street, int pin, String city) {
this.street = street;
this.pin = pin;
this.city = city;
}
}
class EmployeeAddress extends Address {
int empid;
public String empname;
Address add;
EmployeeAddress (int empid, String empname, Address add){
this.empid = empid;
this.empname = empname;
this.add = add;
}
void show() {
System.out.println("my name is " + empname + "and my empid is " + empid);
}
}
class Employee {
public static void main(String ar[]) {
Address ad1 = new Address("mystreet", 201301, "nyk");
EmployeeAddress a1 = new EmployeeAddress(123, "kin", ad1);
a1.show();
}
/*
* public String toString() { return
* "my name is "+a1.empname+"and my pin is "+ad1.pin ; }
*/
}
*請。*改進您的格式和命名,使其符合Java編碼風格約定。 – skiwi
你想在測試中調用什麼地址方法? – JamesB
爲什麼要將父對象作爲參數傳遞給子對象?這是一個非常糟糕的設計。我建議寧願改變你的設計 – Lokesh