【发布时间】:2020-09-22 16:09:05
【问题描述】:
我有一个 AddressBook 类,其属性为 long phoneNumber、Address tempAddress、Address permAddress,其中 Address 是 AddressBook 的内部类。它的属性是名称、城市、街道、州。两个类都有所有属性的 getter 和 setter。
还有另一个类 TestApplication,我需要通过创建 AddressBook 对象并在 main 方法中打印永久、临时地址和电话号码来测试应用程序的工作。
由于 permAddress 和 tempAddress 属性的 getter 和 setter 令人困惑,我无法弄清楚如何将这些 Address 类属性放入 TestApplication。 error description
正确的方法应该是什么?无法实现 Address 类型的属性 tempAddress 和 permAddress 的 getter 和 setter。 到目前为止,这是 AddressBook.java 和 TestApplication.java 的代码。
public class AddressBook{
private long phoneNumber;
private Address tempAddress;
private Address permAddress;
public void setPhoneNumber(long phoneNumber)
{
this.phoneNumber=phoneNumber;
}
public void setTempAddress(Address tempAddress)
{
this.tempAddress=tempAddress;
}
public void setPermAddress(Address permAddress)
{
this.permAddress=permAddress;
}
public long getPhoneNumber()
{
return phoneNumber;
}
public Address getTempAddress()
{
return tempAddress;
}
public Address getPermAddress()
{
return permAddress;
}
class Address{
private String name;
private String street;
private String city;
private String state;
public void setName(String name)
{
this.name=name;
}
public void setCity(String city)
{
this.city=city;
}
public void setStreet(String street)
{
this.street=street;
}
public void setState(String state)
{
this.state=state;
}
public String getName()
{
return name;
}
public String getStreet()
{
return street;
}
public String getCity()
{
return city;
}
public String getState()
{
return state;
}
}
}
import java.util.*;
public class TestApplication {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the permanent address");
System.out.println("Enter the house name");
String hn=sc.nextLine();
System.out.println("Enter the street");
String stee=sc.nextLine();
System.out.println("Enter the city");
String city=sc.nextLine();
System.out.println("Enter the state");
String state=sc.nextLine();
AddressBook a=new AddressBook();
AddressBook.Address b=a.new Address();
b.setName(hn);
b.setStreet(stee);
b.setCity(city);
b.setState(state);
System.out.println("Permanent address");
AddressBook ad=new AddressBook();
System.out.println("House name:"+ad.getPermAddress().getName());
System.out.println("Street:"+ad.getPermAddress().getStreet());
System.out.println("City:"+ad.getPermAddress().getCity());
System.out.println("State:"+ad.getPermAddress().getState());
System.out.println("Enter the temporary address");
System.out.println("Enter the house name");
String house=sc.nextLine();
System.out.println("Enter the street");
String street1=sc.nextLine();
System.out.println("Enter the city");
String city1=sc.nextLine();
System.out.println("Enter the state");
String state1=sc.nextLine();
System.out.println("Enter the phone number");
long ph=sc.nextLong();
System.out.println("Temporary address");
System.out.println("House name:"+ad.getTempAddress().getName());
System.out.println("Street:"+ad.getTempAddress().getStreet());
System.out.println("City"+ad.getTempAddress().getCity());
System.out.println("State:"+ad.getTempAddress().getState());
ad.setPhoneNumber(ph);
System.out.println("Phone number"+ad.getPhoneNumber);
}
}
【问题讨论】:
-
请添加有关您的代码的更多详细信息。只是说它令人困惑并没有给我们很多信息
-
我已经添加了必要的细节。
标签: java inner-classes