toString():用于调试还是用于人类?

编程入门 行业动态 更新时间:2024-10-28 20:22:40
本文介绍了toString():用于调试还是用于人类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 class Address { private enum Component { NUMBER, STREET, STATE, COUNTRY } private Map<Component, String> componentToValue = ...; }

我希望我的课程包含两个方法:

I'd like my class to contain two methods:

  • 一个表示每个地址组件的值(以便我可以调试是否出错).
  • 一个以人类期望的形式返回地址的地址:"1600 Amphitheatre Parkway Mountain View,CA 94043".
  • Object.toString()的最佳做法是什么?它主要用于#1或#2吗?这些方法的命名是否有最佳实践?

    What is the best-practice for Object.toString()? Is it primary meant for #1 or #2? Is there a best-practice for the naming of these methods?

    推荐答案

    您是否会以相同的方式在SMS消息和HTML页面中设置地址格式?您会用英语,法语和日语将其格式化吗?

    Would you format an address the same way in a SMS message and in an HTML page? Would you format it the same way in English, French and Japanese?

    如果否,那么您的答案是:表示不属于对象,而是属于显示对象的表示层.除非对象专门用于表示层,例如,如果它是HtmlI18nedAddress,则使用toString进行调试.

    If no, then you have your answer : the presentation does not belong to the object, but to the presentation layer displaying the object. Unless the object is specifically made up for the presentation layer, for example if it is a HtmlI18nedAddress, use toString for debugging.

    考虑Date与SimpleDateFormat. Date包含状态,SimpleDateFormat返回多个表示形式.

    Consider Date vs SimpleDateFormat. Date contains the state and SimpleDateFormat returns multiple representations.

    更多推荐

    toString():用于调试还是用于人类?

    本文发布于:2023-11-05 14:47:56,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1561133.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:人类   toString

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!