从Bank Class调用的地址类

时间:2013-09-05 18:11:50

标签: java class mutators

我有两个相互延伸的课程。我有一个mutator方法,它调用Address.Java类来从类中获取城市和州。但是Bank.java类给了我一些问题。给我带来问题的具体方法是

public void setBankAddress(String aCity,String aState)(代码如下)我不知道这是否有意义 {

}

Bank.Java

public class Bank
{
     private String bankName;
     private int bankID;
     private Address bankAddress;

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

     public String getBankName()
     {
         return bankName;
     }

     public int getBankID()
     {
         return bankID;
     }

     public Address getBankAddress()
     {
         return bankAddress;
     }

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

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

     public void setBankAddress(String aCity, String aState)
     {

     }

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

}

地址。 Java

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)
    {
    city = aCity;
    }

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

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

}

1 个答案:

答案 0 :(得分:2)

你不知道如何在对象上调用方法吗?

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

bankAddress已在构造函数中初始化,因此您可以安全地调用这些方法。

然而,在这些方法之外做这个并且传递整个对象本身通常是好习惯

Address someAddress = new Address();
someAddress.setCity(aCity);
someAddress.setState(aState);

Bank bank = new Bank();
bank.setBankAddress(someAddress);
...
public void setBankAddress(Address aBankAddress) {
    bankAddress = aBankAddress;
}