is there a way to access an object within an object?

痴心易碎 提交于 2019-12-22 08:55:55

问题


I'm practicing java by building a simple directory. I have 4 Classes. These are:

  1. Person

  2. Address

  3. Contact

  4. testClass

I have already finished creating this system and it works the way I want it. I did this by making 3 different arrays for the Person, Address and Contact. To link the Person, Address and Contact together I place them on there respective array with the same index number. (Not literally linking them together, just a way to know which address or contact to access when editing a person).

But now, I want to optimize it. I want to create a single HashMap to hold a person with the address and contacts within it. Please see my code below for more information.

Person.class

public class Person {
private long Id;
private firtName;
private Address address;
private Contact contact;

//some setter and getter methods

public Person(Address address, Contact contact) {
this.address = address;
this.contact = contact;
}
}

Address.class

public class Address {

private String street;
private String city;
private String province;


//some setter and getter methods
}

Contact.class

public class Contact {

private long Phone;
private String Email;

//some setter and getter methods
}

testClass.class

public class testClass {

public static void main(String[] args) {
HashMap<Integer, Person> person = new HashMap<Integer, Person>();

person.put(1, new Person(new Address(), new Contact)));
person.get(1).setStreet("place");

}

}

My question is, on this line of code in the testClass

person.get(1).setStreet("place");

is there a way to directly access/edit the person's address or contact without creating a separate array or method?


回答1:


private Address address;
private Contact contact;

If you were to make these public instead of private you would be able to access them directly like so:

Person person = new Person(new Address(), new Contact());

Person p = person.get(1);
String city = p.address.getCity();
String email = p.contact.getEmail();

However this violates the principle of encapsulation (you should hide your inner fields and allow access only through methods).

Instead you should create your person class as follows:

public class Person {
    private long Id;
    private firtName;
    private Address address;
    private Contact contact;

//some setter and getter methods

    public Person(Address address, Contact contact) {
        this.address = address;
        this.contact = contact;
    }

    public Address getAddress() {
        return address;
    }

    public Contact getContact() {
        return contact;
    }
}

and access through

Person p = person.get(1);
String city = p.getAddress().getCity();
String email = p.getContact().getEmail();



回答2:


When a person is an aggregation of address, contact informations and so on, you should have getters and setters for those. You'd then write this to set street on a person:

person.getAddress().setStreet("this and that")

and you could also assign a new address whatsoever:

person.setAddress(new Address("streeetname", "cityname"))

This is a common idiom in Java and the way to go. Having public access objects creates new problems since this kind of access cannot be changed without side-effects in all classes using them.




回答3:


What I see here is a dependency inversion issue. Your class Person depends on Address and Contact. In the purest sense the Address and Contact should be Interfaces, so their implementations could be swapped out at runtime.

public class Person {
  private long Id;
  private firstName;
  private Address address;
  private Contact contact;

  public Person(Address address, Contact contact) {
    this.address = address;
    this.contact = contact;
  }

  public Address getAddress() {
    return this.address;
  }

  public setAddress(Address address) {
    this.address = address;
  }

  public Contact getContact() {
    return this.contact;
  }

  public setContact(Contact contact) {
    this.contact = contact;
  }

}

Then you could access Address in the following way:

person.get(1).getAddress().setStreet("place");


来源:https://stackoverflow.com/questions/34459901/is-there-a-way-to-access-an-object-within-an-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!