简体   繁体   中英

Return result from a constructor from another class in java

Here is the first file and there is another to follow, to really explain my question; my question starts in the public Account getbankAddress() method.

public class Bank {

    String bankName;
    int bankID;
    Address address = new Address();

    public Bank(){
        bankName = "?";
        bankID = 0;
    }

    public String getBankName(){
        return bankName;
    }

    public int getBankID(){
        return bankID;
    }

    public Address getBankAddress(){ 
        // This is where I'm having trouble getting the bank address from the address class,
        // How can I produce a result from the mutator method of setCity and setState methods?
        return address;
    }

    public void setBankName(String bankName1){
        bankName = bankName1;
    }

    public void setBankID(int ID){
        bankID = ID;
    }

    public void setBankAddress(String aCity, String aState){
        aCity = "city";
        aState = "state";
    }

    public String toString(){
        String str = ("\nBank name:\t\t" + bankName + "\nBank ID:\t\t" + bankID +
                "\nBank address:\t\t" + bankAddress + "\n\n");
        return str;
    }
}

public class Address{

    private String city;
    private String state;

    public Address(){
        city = "?";
        state = "?";
    }

    public String getCity(){
        return city;
    }

    public String getState(){
        return state;
    }

    public void setCity(String aCity){
        aCity = city;
    }

    public void setState(String aState){
        aState = state;
    }

    public String toString(){
        String str1 = (city + "," + state);
        return str1;
    }
}

I think changing this:

public void setBankAddress(String aCity, String aState){

aCity = "city";
aState = "state";

 }

to

public void setBankAddress(String aCity, String aState) {
  address.setCity(aCity);
  address.setState(aState);
}

should work.

++++++++++++++++

Also, change this:

public void setCity(String aCity){
    aCity = city;
}

public void setState(String aState){
    aState = state;
}

to

public void setCity(String aCity){
    city = aCity;
}

public void setState(String aState){
    state = aState;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM