使用toString的Java语言设计 [英] Java Language Design with toString

查看:59
本文介绍了使用toString的Java语言设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们是否决定不为int[]实现toString方法,而是让它继承ObjecttoString方法?

We did they make the decision to not implement a toString method for int[], but instead let it inherit the toString method from Object?

推荐答案

他们确实为数组实现了更合理的toString方法.它们位于 java.util.Arrays 课.

They did implement more reasonable toString methods for arrays. They are located in the java.util.Arrays class.

至于推理.我假设通过Arrays类中提供的重写,尝试为不同类型的数组实现通用的toString是复杂的还是不可能的. toString方法必须知道它正在处理哪种类型的数组,并适当地输出数据.例如,Object[]必须在每个元素上使用toString,而char[]必须输出字符,并且数字数据类型必须转换为数字字符串.

As for reasoning. I'm assuming by the overrides provided in the Arrays class, that trying to implement a generic toString for different types of arrays is either complex or impossible. The toString method would have to know what type of array it was working on, and output the data approrpiately. For instance, Object[] must use toString on each element, while char[] must output the character, and the numeric datatypes must be converted to a numeric string.

Arrays中的方法免费获得此方法,因为由于覆盖而导致类型固定.

The methods in Arrays get this for free, because the types are fixed due to the overrides.

这篇关于使用toString的Java语言设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆