在 Java 中是否可以覆盖 Objects 数组的“toString”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10295600/
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
Is it possible in Java to override 'toString' for an Objects array?
提问by Popokoko
Is it possible in Java to override a toString for an Objects array?
是否可以在 Java 中覆盖 Objects 数组的 toString?
For example, let's say I created a simple class, User
(it doesn't really matter which class is it since this is a general question). Is it possible that, once the client creates a User[]
array and the client uses System.out.print(array)
, it won't print the array's address but instead a customized toString()
?
例如,假设我创建了一个简单的类User
(它是哪个类并不重要,因为这是一个通用问题)。是否有可能,一旦客户端创建了一个User[]
数组并且客户端使用了System.out.print(array)
,它就不会打印数组的地址,而是打印自定义的toString()
?
PS: of course I can't just override toString()
in my class since it's related to single instances.
PS:当然,我不能只toString()
在我的班级中进行覆盖,因为它与单个实例有关。
回答by Theodore Norvell
No. Of course you can create a static method User.toString( User[] ), but it won't be called implicitly.
不。当然你可以创建一个静态方法 User.toString( User[] ),但它不会被隐式调用。
回答by Deco
You can use Arrays.toString(Object[] a);
which will call the toString()
method on each object in the array.
您可以使用Arrays.toString(Object[] a);
which 将调用toString()
数组中每个对象的方法。
Edit(from comment):
编辑(来自评论):
I understand what it is you're trying to achieve, but Java doesn't support that at this time.
我了解您想要实现的目标,但 Java 目前不支持。
In Java, arrays are objects that are dynamically created and may be assigned to variables of type Object. All methods of class Object may be invoked on an array. See JLS Ch10
在 Java 中,数组是动态创建的对象,可以分配给 Object 类型的变量。Object 类的所有方法都可以在数组上调用。见JLS Ch10
When you invoke toString()
on an object it returns a string that "textually represents" the object. Because an array is an instance of Object that is why you only get the name of the class, the @ and a hex value. See Object#toString
当您调用toString()
一个对象时,它会返回一个“文本表示”该对象的字符串。因为数组是 Object 的一个实例,所以您只能获得类的名称、@ 和十六进制值。见对象#toString
The Arrays.toString()method returns the equivalent of the array as a list, which is iterated over and toString()
called on each object in the list.
所述Arrays.toString()方法返回数组作为列表,其被遍历并相当于toString()
称为列表中的每个对象上。
So while you won't be able to do System.out.println(userList);
you can do System.out.println(Arrays.toString(userList);
which will essentially achieve the same thing.
因此,虽然您将无法做到,但System.out.println(userList);
您可以做到System.out.println(Arrays.toString(userList);
这基本上可以实现相同的目标。
回答by Jeroen
You can create a separate class containing the array, and override toString()
.
您可以创建一个包含数组的单独类,并覆盖toString()
.
I think the simplest solution is to extend the ArrayList
class, and just override toString()
(for example, UserArrayList
).
我认为最简单的解决方案是扩展ArrayList
类,然后覆盖toString()
(例如,UserArrayList
)。
回答by Stan Fad
Try this
试试这个
User[] array = new User[2];
System.out.println(Arrays.asList(array));
of course if you have customized user.toString()
method
当然如果你有定制的user.toString()
方法
回答by Peter Lawrey
The only way you can do this is to re-compile Object.toString()
and add instanceof
clauses.
您可以这样做的唯一方法是重新编译Object.toString()
和添加instanceof
子句。
I had requested a change in Project Coin to handle arrays in a more object orientated way. I felt that it's too much for beginners to learn all the functionality you need in Array, Arrays and 7 other helper classes which are commonly used.
我曾要求对 Project Coin 进行更改,以便以更加面向对象的方式处理数组。我觉得对于初学者来说,在 Array、Arrays 和其他 7 个常用的辅助类中学习您需要的所有功能太多了。
I think in the end it was concluded that to make arrays properly object orientated is a non-trivial task which will be pushed back to Java 9 or beyond.
我认为最后得出的结论是,使数组正确地面向对象是一项重要的任务,它将被推回到 Java 9 或更高版本。
回答by Jakub Zaverka
You cannot do that. When you declare an array, then Java in fact creates a hidden object of type Array. It is a special kind of class (for example, it supports the index access [] operator) which normal code cannot directly access.
你不能这样做。当您声明一个数组时,Java 实际上会创建一个 Array 类型的隐藏对象。它是一种特殊的类(例如,它支持索引访问 [] 运算符),普通代码无法直接访问。
If you wanted to override toString(), you would have to extend this class. But you cannot do it, since it is hidden.
如果要覆盖 toString(),则必须扩展此类。但是你不能这样做,因为它是隐藏的。
I think it is good to be it this way. If one could extend the Array class, then one could add all kinds of methods there. And when someone else manages this code, they see custom methods on arrays and they are "WTF... Is this C++ or what?".
我认为这样很好。如果可以扩展 Array 类,那么就可以在那里添加各种方法。当其他人管理这段代码时,他们看到数组上的自定义方法,他们是“WTF ...这是 C++ 还是什么?”。