什么时候不希望在Java中实现toString()? [英] When is it desired to not implement toString() in Java?

查看:87
本文介绍了什么时候不希望在Java中实现toString()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我项目的首席开发人员已经将项目的toString()实现称为pure cruft,并希望将其从代码库中删除。

A lead developer on my project has taken to referring to the project's toString() implementations as "pure cruft" and is looking to remove them from the code base.

我已经说过,这样做意味着任何希望显示对象的客户都必须编写自己的代码来将对象转换为字符串,但是回答是的,他们会。

I've said that doing so would mean that any clients wishing to display the objects would have to write their own code to convert the object to string, but that was answered with "yes they would".

现在具体来说,这个系统中的对象是图形元素,如矩形,圆形等,当前表示是显示x,y,比例,边界等...

Now specifically, the objects in this system are graphic elements like rectangles, circles, etc and the current representation is to display x, y, scale, bounds, etc...

那么,人群在哪里撒谎?

So, where does the crowd lie?

你应该什么时候和什么时候不应该实现toString?

When should you and when shouldn't you implement toString?

推荐答案

他们有什么害处?为什么要删除它们?我发现toString()在发出调试语句时非常有用。

What harm do they do? Why remove them if you have them? I find toString() extremely useful when emitting debugging statements.

就我个人而言,我总是错误地认为有一个可行的toString()方法。这么少的工作要写。

Personally, I would always err on the side of having a workable toString() method. So little work to write.

这篇关于什么时候不希望在Java中实现toString()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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