getRichStringCellValue().getString()和getRichStringCellValue().toString()之间的区别是什么

Arv*_*ani 0 java string excel apache-poi

我目前正在使用Apache POI处理MS Excel文件,我遇到了一些看起来很奇怪的东西.HSSFCell类有一个getRichStringCellValue()返回HSSFRichTextString对象的方法.为了将其转换为普通字符串,我们使用getString().看起来像这样:

cell.getRichStringCellValue().getString()
Run Code Online (Sandbox Code Playgroud)

现在是什么让我疑惑是,为什么有一个getString()摆在首位,在toString()做实质上是一样的,即返回字符串相同呢?有谁知道为什么?

Hon*_*dek 5

合同toString():

toString method returns a string that "textually represents" this object
Run Code Online (Sandbox Code Playgroud)

一般目的toString()是提供对象的人类可读表示.有些类更进一步,并指定了详细的格式toString(),这通常是不可取的,如果是,那么只针对"值对象"(日期,时间,数字).

除非在类的javadoc中指定,否则不应该依赖于返回的文本的特定格式toString().

一般建议:如果您的类想要引入特定的持久性文本表示,则应该通过单独的方法提供.因此,如果toString()稍后实施更改,您永远不会感到不快.