在 Java 中是否可以覆盖 Objects 数组的“toString"? [英] Is it possible in Java to override 'toString' for an Objects array?
问题描述
在 Java 中是否可以覆盖对象数组的 toString?
Is it possible in Java to override a toString for an Objects array?
例如,假设我创建了一个简单的类,User
(它是哪个类并不重要,因为这是一个通用问题).是否有可能,一旦客户端创建了一个 User[]
数组并且客户端使用了 System.out.print(array)
,它不会打印数组的地址,而是而是自定义的 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()
?
PS:当然,我不能在我的类中只覆盖 toString()
,因为它与单个实例相关.
PS: of course I can't just override toString()
in my class since it's related to single instances.
推荐答案
没有.当然你可以创建一个静态方法 User.toString( User[] ),但它不会被隐式调用.
No. Of course you can create a static method User.toString( User[] ), but it won't be called implicitly.
这篇关于在 Java 中是否可以覆盖 Objects 数组的“toString"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!