我的代码:
public abstract class Address {
public String myFirstname;
public String myLastname;
public String myStreet;
public String myCity;
public String myState;
public int myZipcode;
public Address(String firstname, String lastname, String street, String city, String state, int zipcode) {
myFirstname = firstname;
myLastname = lastname;
myStreet = street;
myCity = city;
myState = state;
myZipcode = zipcode;
}
public void fullAddress() {
System.out.println(myFirstname + " " + myLastname);
System.out.println(myStreet + "\n" + myCity + ", " + myState + " " + myZipcode);
}
public void setName(String f, String l) {
myFirstname = f;
myLastname = l;
}
public void setStreet(String s) {
myStreet = s;
}
public void setCity(String c) {
myCity = c;
}
public void setState(String sta) {
myState = sta;
}
public void setzipcode(int z) {
myZipcode = z;
}
}
public class Boss extends Address {
public int myRoomnumber;
public Boss(String firstname, String lastname, String street, String city, String state, int zipcode, int roomnumber) {
super(firstname, lastname, street, city, state, zipcode);
myRoomnumber = roomnumber;
}
public void setRoomnumber(int room) {
myRoomnumber = room;
}
public void fullAddress() //method overriding
{
System.out.println(myFirstname + " " + myLastname);
System.out.println(myStreet + "\n" + myCity + ", " + myState + " " + myZipcode + "\n" + "Roomnumber:" + myRoomnumber);
}
class Worker extends Boss {
public String mySection;
public Worker(String firstname, String lastname, String street, String city, String state, int zipcode, int roomnumber, String section) {
super(firstname, lastname, street, city, state, zipcode, roomnumber);
mySection = section;
}
public void setRoomnumber(int room, String s) //method overloading
{
myRoomnumber = room;
mySection = s;
}
public void fullAddress() {
System.out.println(myFirstname + " " + myLastname);
System.out.println(myStreet + "\n" + myCity + ", " + myState + " " + myZipcode + "\n" + "myRoomnumber:" + myRoomnumber + "-" + mySection);
}
}
class Security extends Address {
public int myRoomnumber;
public Security(String firstname, String lastname, String street, String city, String state, int zipcode, int roomnumber) {
super(firstname, lastname, street, city, state, zipcode);
myRoomnumber = roomnumber;
}
public void fullAddress() {
System.out.println(myFirstname + "" + myLastname + "\n" + "Roomnumber:" + myRoomnumber);
}
}
}
在我的main
方法中,当我创建一个Boss
实例时,它可以正常运行并提供fulladdress
但是当我尝试创建Worker
实例时,它会给我错误
Boss we = new Boss("asdf","sdfsdf","sdfsdf","sdfsdf","sdfsdfs",232,235);
we.fullAddress();
Worker ww = new Worker("asdf","sdfsdf","sdfsdf","sdfsdf","sdfsdfs",232,235,"sdfs");
ww.fullAddress();
答案 0 :(得分:2)
Work
是一个"实例,内部类" Boss
,这意味着Worker
无法在没有Boss
实例的情况下实例化
class Boss extends Address {
//...
class Worker extends Boss {
首先从Worker
内删除Boss
,以便它更多的是自己的类
class Boss extends Address {
//...
}
class Worker extends Boss {
//...
}
答案 1 :(得分:0)
您必须使用Boss类型的封闭实例限定分配。
Boss we = new Boss("asdf","sdfsdf","sdfsdf","sdfsdf","sdfsdfs",232,235);
we.fullAddress();
Worker ww = we.new Worker("asdf", "sdfsdf", "sdfsdf", "sdfsdf", "sdfsdfs", 232, 235, "sdfs");
ww.fullAddress();