【问题标题】:How to call the getters and setters of inner class attributes in the TestApplication.java class?TestApplication.java类中如何调用内部类属性的getter和setter?
【发布时间】: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


【解决方案1】:

所以 getter 和 setter 是非静态方法(在大多数情况下)。这意味着您必须实例化对象,然后调用方法。所以你要做的是创建地址实例Address a = new Address (...);,然后你可以使用该实例来调用你的getter和setter,即a.getZipCode();

【讨论】:

    【解决方案2】:

    在接受用户输入时使用sc.next() 而不是sc.nextLine()

    【讨论】:

      【解决方案3】:

      因为内部类不是公开的,所以您需要获取 Adress 属性,例如: gettempAdresse().getName();

      首先,您使用“gettempAdresse()”返回一个 Adresse 类型的对象,然后调用该对象的“getName()”方法返回名称。

      编辑:

      您需要先将constructors 添加到每个班级。

      public AddressBook() {
              tempAddress = new Address();
              permAddress = new Address();
          }
      
      public Address(String name, String street, String city, String state) {
              this.name = name;
              this.street = street;
              this.city = city;
              this.state = state;
      }
      
      public Address() {
          this("", "", "", "");
      }
      

      在此之后,您可以创建一个 AddressBook 的实例并像这样使用 getter 和 setter:

      AddressBook addressBook = new AddressBook();                    //create instence of AddressBook
      
      addressBook.setPermAddress(addressBook.new Address());          //set permAddress to a new Address
      addressBook.getPermAddress().setName("name of permAddress");    //set name of permAddress
      

      【讨论】:

      • 如何先设置settempAddress()中的值?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-21
      • 2011-04-27
      • 2019-06-04
      • 1970-01-01
      • 2019-11-26
      相关资源
      最近更新 更多