java - 返回对象值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4530531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java - return object value
提问by Jake
Hi i have the following code:
嗨,我有以下代码:
public List<Person> findAll() {
List<Person> copy = new ArrayList<Person>();
for (Person person : personer) {
copy.add(person);
}
return copy;
}
But when i test this i only retrieve the following and not the value:
但是当我测试这个时,我只检索以下而不是值:
[Person@15c7850, Person@1ded0fd, Person@16a9d42]
[人@15c7850,人@1ded0fd,人@16a9d42]
How do i get the values and not like above. Where i am inserting the person the code looks like this:
我如何获得值而不是像上面那样。我在哪里插入代码如下所示:
public boolean insert(String name, String nbr) {
if (containsName(name)) {
return false;
}
Person person = new Person(name, nbr);
personer.add(person);
return true;
}
and here is my Person class:
这是我的 Person 类:
class Person {
private String name;
private String nbr;
public Person (String name, String nbr) {
this.name = name;
this.nbr = nbr;
}
public String getName() {
return name;
}
public String getNumber() {
return nbr;
}
}
回答by darioo
You're already receiving the objects you want.
您已经收到了您想要的对象。
What you see is an internal representation of these objects.
您看到的是这些对象的内部表示。
You must iterate through them and call their respective methods to see the information you probably want to see.
您必须遍历它们并调用它们各自的方法以查看您可能想要查看的信息。
If you're not satisfied with these results, you must override toString
to provide you with more meaningful information.
如果您对这些结果不满意,则必须覆盖toString
以向您提供更有意义的信息。
Update:
更新:
after seeing your edit, you should add toString
similar to this one in your Person
class:
看到你的编辑后,你应该toString
在你的Person
班级中添加类似的内容:
@Override
public String toString() {
return "Name: " + name + ", number: " + nbr;
}
By the way, you're storing nbr
as a string, and it's obvious it should be an integer. So, I'd suggest changing its type to an int
or Integer
.
顺便说一句,您存储nbr
为一个字符串,很明显它应该是一个整数。因此,我建议将其类型更改为int
or Integer
。
回答by Corv1nus
You are getting a List object back. You can use the Person object to get the data that you need. To get to the Person objects, iterate over the list.
您正在返回一个 List 对象。您可以使用 Person 对象来获取您需要的数据。要访问 Person 对象,请遍历列表。
List<Person> people = findAll();
for Person p : people {
String phoneNumber = p.phoneNumber();
String name = p.Name();
}
回答by thejh
Put something like this in the class Person
(don't change the method name!):
在类中放入类似的内容Person
(不要更改方法名称!):
public String toString() {
return name;//change this line
}
回答by Ian Bishop
You are printing out an Object that has the default toString inherited from the Object class. This will print out the type of object it is and its location in memory (ie: Person@1ded0fd).
您正在打印一个对象,该对象具有从 Object 类继承的默认 toString。这将打印出它的对象类型及其在内存中的位置(即:Person@1ded0fd)。
If you'd like it to see something else, you can override the toString method within your class:
如果你想让它看到别的东西,你可以在你的类中覆盖 toString 方法:
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public String toString() {
return this.name;
}
}
If your class looked like the above, this would allow you to do something like this:
如果你的类看起来像上面那样,这将允许你做这样的事情:
Person p = new Person("John");
System.out.println(p);
> John
You can also just grab it as is and print out any information you want from it without overriding the toString method.
您也可以按原样获取它并打印出您想要的任何信息,而无需覆盖 toString 方法。
Person p = new Person("John");
System.out.println(p.getName());
> John
回答by jzd
Override the toString() method in the Person class if you want a better description when printing the results.
如果在打印结果时需要更好的描述,请覆盖 Person 类中的 toString() 方法。
回答by Vince
What value or class Person's property you aspect to retrieve from the ArrayList? This kind of value(Person@15c7850, etc) shows that the Person's object random id that assigned by JVM when you use
您希望从 ArrayList 中检索什么值或类 Person 的属性?这种值(Person@15c7850 等)表明使用时由 JVM 分配的 Person 对象随机 id
System.out.print(copy)
.
System.out.print(copy)
.