例如,请查看以下代码中的两个方法:
public class Customer {
private Address address;
public Customer() {
address = null;
}
public void setAddress(int houseNumber, String postcode) {
address = new Address(houseNumber, postcode);
}
public String getPostcode() {
if (address == null) {
return null;
} else {
return postcode;
}
}
“返回邮政编码;”代码行不正确,我知道这一点,但我想只将setAddress方法的邮政编码返回到getPostcode()方法。我该怎么做?
答案 0 :(得分:0)
我猜它是XY problem。您有一个类Address
,更有可能包含字段houseNumber
和postCode
。因此,要获得postCode
,您需要为postCode
课程中的Adress
生成getter,只需编写:return address.getPostCode()