Java帮助实例无法正常工作

时间:2014-05-27 03:38:11

标签: java

我的代码:

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

2 个答案:

答案 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();