Is there a way to access an object inside an object?

I do Java by creating a simple directory. I have 4 classes. It:

  • Person

  • The address

  • Contact

  • 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 Person, address and contact. To associate an individual, address and contact together, I put them in the corresponding array with the same index number. (Not literally linking them, just a way to find out 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 an address and contacts inside it. 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 testClass

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

/ ?

+4
3
private Address address;
private Contact contact;

public private, :

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

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

( ).

:

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;
    }
}

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

, .., . , :

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

:

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

Java . , , .

+1

I see the problem of dependency inversion here. Your class Persondepends on Addressand Contact. In the purest sense, the address and contact must be interfaces, so their implementation can be replaced at run time.

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 can access Addressas follows:

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

Source: https://habr.com/ru/post/1621677/


All Articles