我有这段代码:
.setAutoCancel(false)
我需要在另一个类的构造函数中调用变量postcode,town和street:
public class Address
{
// instance variables
private String postcode;
private String street;
private String town;
/**
* Constructor for objects of class Address
*/
public Address(String street, String town, String postcode)
{
// initialise instance variables
this.street = street;
this.town = town;
this.postcode = postcode;
}
public String getPostcode()
{
return postcode;
}
public String getStreet()
{
return street;
}
public String getTown()
{
return town;
}
public void printAddress()
{
System.out.println(street);
System.out.println(town);
System.out.println(postcode);
}
public void setFullAddress(String street, String town, String postcode)
{
this.street = street;
this.town = town;
this.postcode = postcode;
}
public void setPostcode(String postcode)
{
this.postcode = postcode;
}
public void setStreet(String street)
{
this.street = street;
}
我尝试过以某种方式做到这一点,但是没有找到正确调用它们的方法,因此当创建对象借用者时,它会从地址类中获取邮政编码,城镇和街道。任何帮助表示赞赏。
答案 0 :(得分:0)
您可以使用:
address.setFullAddress(street,town,postcode);
之后,您可以在任何地方使用address
类的get方法。
答案 1 :(得分:0)
一种方法是直接使用类Address
的对象调用构造函数:
public Borrower(String fName ,String lName ,String lNumber, Address addr)
{
...
address = addr;
}
其他方法是在构造函数中创建对象:
public Borrower(String fName ,String lName ,String lNumber, String town, String street, String postcode)
{
...
address = new Address(town, street, postcode);
}
答案 2 :(得分:0)
在第二类的构造中,只需传递一个Address类的引用。
Public Borrower(Address address){
this.address=address;
}
现在添加Address的getter方法,然后你可以访问Address类的所有实例变量。