使用 toString 进行 Java 语言设计
我们决定不为 int[]
实现 toString
方法,而是让它从 继承
?toString
方法对象
We did they make the decision to not implement a toString
method for int[]
, but instead let it inherit the toString
method from Object
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
他们确实为数组实现了更合理的 toString 方法。它们位于
java.util 中。数组
类。至于说理。我假设通过 Arrays 类中提供的重写,尝试为不同类型的数组实现通用的 toString 要么很复杂,要么是不可能的。 toString 方法必须知道它正在处理什么类型的数组,并适当地输出数据。例如,
Object[]
必须在每个元素上使用toString
,而char[]
必须输出字符,并且必须转换数字数据类型为数字字符串。Arrays
中的方法免费获得此值,因为类型由于覆盖而固定。They did implement more reasonable
toString
methods for arrays. They are located in thejava.util.Arrays
class.As for reasoning. I'm assuming by the overrides provided in the
Arrays
class, that trying to implement a generictoString
for different types of arrays is either complex or impossible. ThetoString
method would have to know what type of array it was working on, and output the data approrpiately. For instance,Object[]
must usetoString
on each element, whilechar[]
must output the character, and the numeric datatypes must be converted to a numeric string.The methods in
Arrays
get this for free, because the types are fixed due to the overrides.我猜是因为以下原因:他们如何知道用户想要如何展示他们的数组?它可能是“数组大小:10”,也可能是“[x,y,z]”。
他们给了你一个默认值,如果你想把它变成别的东西,很容易做到。
您可以使用 apache 的 ToStringBuilder 使其更容易...
http://commons.apache.org/lang/api/org/apache/commons/lang/builder/ToStringBuilder.html
I guess because of the following reasoning: how would they know how users would want to present their array? It might "array size: 10" or it might be "[x,y,z]".
They gave you a default, if you want to make it something else it is easy to do.
You can use apache's ToStringBuilder make it easier...
http://commons.apache.org/lang/api/org/apache/commons/lang/builder/ToStringBuilder.html
我的猜测是,因为数组对象不是由语言设计者在 Java 源代码中创建的 - 它们是由 Java 编译器创建的。请记住,您可以拥有任何对象类型的数组,因此编译器会根据您需要的类型创建数组对象。
如果他们要创建一个标准方法,那么它应该如何工作并不是立即显而易见的。例如,执行
toString()
并连接结果对于小型数组可能没问题,但对于多维数组或具有 1,000 个条目的数组则不起作用。所以我认为没有创建 toString() 方法来保持所有数组的一致性。不可否认,这很烦人,有时我确实认为类似于
"Array[" + size + "] of " + getClassName()
的东西会比默认值好得多。My guess would be is that because Array objects weren't created in Java source code by the language designers - they are created by the Java compiler. Remember, you can have an array of any object type and so the compiler creates the Array object as appropriate for the type you require.
If they were to create a standard method, it's not immediately obvious how this should work. For example, executing
toString()
and concatenating the results might be OK for a small array but it doesn't work for a multidimensional array or an array with 1,000 entries. So I think notoString()
method is created to keep all arrays consistent.Admittedly, it is annoying and sometimes I do think something along the lines of
"Array[" + size + "] of " + getClassName()
would be so much better than the default.这里有一些猜测,但是......
没有明显的 int 数组的字符串表示。人们以不同的方式进行操作:用逗号分隔、用空格分隔、用方括号或圆括号括起来或什么也不包含。这可能促使我们决定不在 Java 1.1 中实现它,而且它是低优先级代码(因为任何人都可以非常简单地实现将数组编写为字符串的方法)。
现在您无法在 Java 1.2 或更高版本中升级它,因为这会破坏已经使用旧行为的任何人的兼容性。不过,您可以添加一个实现某些功能的实用程序类,这就是他们对 java.util.Arrays 所做的事情。
A bit of guesswork here, but...
There isn't an obvious string representation of an int array. People do it in different ways: comma separated, space separated, enclose in brackets or parentheses or nothing. That probably drove the decision not to implement it in Java 1.1, along with it being low priority code (since anyone can implement a method to write an array as a string themselves very simply).
Now you can't upgrade it in Java 1.2 or later because that would break back compatibility for anyone already using the old behaviour. You can however add a utility class that implements some functionality, and that's what they did with java.util.Arrays.