有没有一种方法可以访问对象中的对象? [英] is there a way to access an object within an object?
问题描述
我正在通过构建一个简单的目录来练习Java。我有4节课。这些是:
I'm practicing java by building a simple directory. I have 4 Classes. These are:
-
人员
Person
地址
联系
testClass
testClass
我已经完成了该系统的创建,并且可以按照我想要的方式工作。为此,我为人,地址和联系人创建了3个不同的数组。为了将人,地址和联系人链接在一起,我将它们放在具有相同索引号的相应数组上。 (并不是从字面上将它们链接在一起,只是一种在编辑人员时知道要访问哪个地址或联系人的方式。)
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).
但是现在,我想对其进行优化。我想创建一个HashMap来容纳一个带有地址和联系人的人。有关更多信息,请参见下面的代码。
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
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
Address.class
public class Address {
private String street;
private String city;
private String province;
//some setter and getter methods
}
Contact.class
Contact.class
public class Contact {
private long Phone;
private String Email;
//some setter and getter methods
}
testClass .class
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");
}
}
我的问题是,在testClass的这一行代码上
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?
推荐答案
private Address address;
private Contact contact;
如果要公开这些
的 private
您可以像这样直接访问它们:
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;
}
}
并通过
Person p = person.get(1);
String city = p.getAddress().getCity();
String email = p.getContact().getEmail();
这篇关于有没有一种方法可以访问对象中的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!