`
leonzhx
  • 浏览: 786042 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Item 10: Always override toString

阅读更多

1.    The string returned by Object.toString consists of the class name followed by an “at” sign (@) and the unsigned hexadecimal representation of the hash code.


2.    Providing a good toString implementation makes your class much more pleasant to use. (The client of your class will generate diagnostic messages in well-formed fashion.)

 

3.    It is impractical if the object is large or if it contains state that is not conducive to string representation. Under these circumstances, toString should return a summary such as ”Thread[main,5,main]”.

 

4.    The advantage of specifying the format of toString result is that it serves as a standard, unambiguous, human-readable representation of the object. It is recommended that you do this for value classes. If you specify the format, it’s usually a good idea to provide a matching static factory or constructor so programmers can easily translate back and forth between the object and its string representation. This approach is taken by many value classes in Java including BigInteger, BigDecimal and most of the boxed primitive classes.


5.    The disadvantage of specifying the format of the toString return value is that once you’ve specified it, you’re stuck with it for life, assuming your class is widely used. Programmers will write code to parse the representation, to generate it, and to embed it into persistent data.


6.    Whether or not you specify the format, provide programmatic access to all of the information contained in the value returned by toString.


7.    Whether or not you decide to specify the format, you should clearly document your intentions. (Give some example of the string you might return)

分享到:
评论

相关推荐

    Effective Java 3rd edition(Effective Java第三版英文原版)附第二版

    Item 12: Always override toString Item 13: Override clone judiciously Item 14: Consider implementing Comparable 4 Classes and Interfaces Item 15: Minimize the accessibility of classes and members Item...

    Delphi面向对象:overload与override[文].pdf

    Delphi面向对象编程之overload与override Delphi面向对象编程中,overload和override是两个非常重要的概念,它们都是面向对象编程的基础概念。本文将详细介绍overload和override的概念、应用场景及实现方法。 一、...

    Android代码-CityPickerWebView

    @Override public void getCity(final String name) { textView.setText(name); } 处理返回键: @Override public void onBackPressed() { if (cityPicker.isShow()){ cityPicker.close(); return; } ...

    Effective C#

    **Item 10: Use Optional Parameters to Minimize Method Overloads** - **Purpose:** Optional parameters allow for flexible method signatures without cluttering the API with multiple overloads. - **...

    Override错误已解决

    ### Override 错误详解与解决方案 #### 一、概述 在进行 Java 开发的过程中,特别是在使用 Eclipse 这样的集成开发环境(IDE)时,开发者可能会遇到一个常见的问题:`Override` 错误。这种错误通常发生在尝试覆盖...

    android @override 报错解决方案

    现象: … java: 1801: method does not override a method from its superclass @Override… 原因: Eclipse is defaulting to Java 1.5 and you have classes implementing interface methods (which in Java 1.6 ...

    万能的toString

    在Java编程语言中,`toString()`方法是一个非常重要的工具,它允许对象转化为字符串形式,方便在控制台打印、日志记录或与其他字符串进行拼接。本文将深入探讨`toString()`方法,以及它在Java中的应用。 `toString...

    new,override,virtual 示例

    在C#编程语言中,`new`、`override`和`virtual`是三个非常重要的关键字,它们用于控制类成员(主要是方法)的行为,涉及到多态性这一核心概念。接下来我们将详细探讨这三个关键字以及它们在实际编程中的应用。 1. `...

    重写toString和equals方法

    Java 对象的toString和equals方法重写 在 Java 中,每个对象都继承自 Object 类,而 Object 类中定义了两个重要的方法:toString() 和 equals()。这两个方法都是非常重要的,它们分别用于对象的字符串表示和对象...

    java toString ToStringBuilder ReflectionToStringBuilder

    在Java编程中,`toString()`方法是一个非常关键的成员函数,它用于返回对象的字符串表示形式,通常包含类名和对象的属性值。当我们需要打印或调试对象时,`toString()`方法尤其有用。本篇文章将深入探讨几种在Java中...

    new、abstract、virtual、override,sealed关键字区别和使用代码示例

    C# 中的 new、abstract、virtual、override、sealed 关键字的区别和使用代码示例 摘要:本资源旨在对 C# 中的 new、abstract、virtual、override、sealed 关键字进行详细的解释和比较,并提供实践代码示例,以帮助...

    bean运行时跟综实例-TOSTRING

    在Java编程中,`toString()`方法是一个非常关键的工具,特别是在进行调试和日志记录时。标题中的"bean运行时跟综实例-TOSTRING"强调了在运行时通过重写`toString()`方法来追踪和诊断问题的重要性。描述指出,这样做...

    method-override:覆盖HTTP动词

    $ npm install method-override API 注意在需要了解请求方法的任何模块之前使用此模块非常重要(例如,必须在csurf模块之前使用它)。 methodOverride(getter,选项) 创建一个新的中间件函数,以新值覆盖req....

    ListView选中item颜色改变

    "ListView选中item颜色改变"是常见的需求,它涉及到ListView的交互设计,使得用户可以通过视觉反馈感知到所选中的条目。本篇文章将深入探讨如何实现这个功能。 首先,我们需要了解ListView的基本工作原理。ListView...

    对话框VC源代码:override_fontdlg

    在VC++编程环境中,"override_fontdlg"是一个特定的对话框功能实现,它涉及到了对话框的自定义和字体选择的交互。对话框是Windows应用程序中常见的一种用户界面元素,通常用于收集用户输入或者显示一些信息。在这个...

    《 Menu详解(二):利用XML生成菜单和子菜单》源码

    每个菜单项通常用`<item>`标签表示,如下: ```xml <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/action_item1" android:icon="@drawable/ic_item1" android:...

    android 菜单演示

    子菜单的点击事件处理与普通菜单类似,但需要确保菜单项具有子菜单,并在`onOptionsItemSelected(MenuItem item)`中检查子菜单的存在。 通过以上的实践,你可以创建丰富的菜单结构,提升Android应用的用户体验。在...

    C#示例:装箱、拆箱、自定义数据类型转换、ToString方法重载、类方法扩展

    public override string ToString() { return $"这是一个{typeof(MyNumber)},值是{value}"; } ``` 现在,当我们调用`myNum.ToString()`,会返回自定义的字符串。 4. 类方法扩展 C#允许通过`static`类和`this`...

    override abstract virtual interface比较

    C# 中 override、abstract、virtual、interface 的比较 在 C# 编程语言中,override、abstract、virtual、interface 是四个关键字,它们之间存在着紧密的联系,我们经常会遇到 override、abstract、virtual 这三个...

    Android实现二级菜单和标题栏菜单

    android:id="@+id/action_item1" android:title="一级菜单项1"/> <item android:id="@+id/action_item2" android:title="一级菜单项2"/> ``` 2. 在`Activity`中实现`onCreateOptionsMenu()`: ```java @...

Global site tag (gtag.js) - Google Analytics