toString()方法:

java.lang.Object类的toString()方法的定义如下:

public String toString(){

return getClass().getName()+"@"+Integer.toHexString(hashCode());

}

1.当打印一个对象的引用时,实际上默认调用的就是这个对象的toString()方法

2.当打印的对象所在的类没有重写Object中的toString()方法时, 那么调用的就是Object中定义toString()方法。

返回此对象所在的类及对应的堆空间实体的首地址值。

3.当打印的对象所在的类重写了toString()方法时,调用的就是我们自己重写的toString()方法;

注:常这样重写:将对象的属性信息返回,

4.像String类   包装类,File类  Date类等,已经实现了Object类中toString()方法的重写。

TestToString:

package com.aff.equals;

import java.util.Date;

import com.aff.java1.Person;

public class TestToString {
public static void main(String[] args) {
Person p1 = new Person("AA", 10);
System.out.println(p1.toString());// com.aff.java1.Person@6d06d69c
System.out.println(p1);// com.aff.java1.Person@6d06d69c String str = "AA";
String str1 = new String("BB");
System.out.println(str);
System.out.println(str1.toString()); Date d = new Date();
System.out.println(d);
} } 输出结果:

com.aff.java1.Person@6d06d69c
com.aff.java1.Person@6d06d69c
AA
BB
Wed Mar 18 19:14:58 CST 2020

 

注意:double类型转为String类型 :String.valueOf(radius)

e2:

Circle:

package com.aff.equals;

public class Circle extends GeometricObject {
private double radius; public Circle() {
super();
this.radius = 1.0;
} public Circle(double radius) {
super();
this.radius = radius;
} public Circle(double radius, String color, double weight) {
super(color, weight);
this.radius = radius;
} public double getRadius() {
return radius;
} public void setRadius(double radius) {
this.radius = radius;
} // 圆的面积
public double findArea() {
return Math.PI * radius * radius;
} // 重写equals()方法和toString() public boolean equals(Object obj) {
if (this == obj) {
return true;
} else if (obj instanceof Circle) {
Circle c = (Circle) obj;
return this.radius == c.radius;
} else {
return false;
}
} @Override
public String toString() {
return "Circle [radius=" + radius + "]";
} /* public String toString() {
return radius + "";
}*/ }

GeometricObject:

package com.aff.equals;

public class GeometricObject {
protected String color;
protected double weight; public GeometricObject() {
super();
this.color = "white";
this.weight = 1.0;
} public GeometricObject(String color, double weight) {
super();
this.color = color;
this.weight = weight;
} public String getColor() {
return color;
} public void setColor(String color) {
this.color = color;
} public double getWeight() {
return weight;
} public void setWeight(double weight) {
this.weight = weight;
} }

TestCircle:

package com.aff.equals;

public class TestCircle {
public static void main(String[] args) {
Circle c1 = new Circle(2.3);
Circle c2 = new Circle(2.3);
System.out.println(c1.equals(c2));// false-->true
System.out.println(c1.toString());
}
} 输出结果:

true
Circle [radius=2.3]

 

toString()方法的使用的更多相关文章

  1. 采用重写tostring方法使ComboBox显示对象属性

    当ComboBox中添加的是对象集合的时候,如果运行就会发现显示是的命令空间.类名,而如果我们想显示对象属性名的时候,我们就可以在对象类中重写object基类中的tostring方法.

  2. ECMAScript toString() 方法

    ECMAScript 定义所有对象都有 toString() 方法,无论它是伪对象,还是真对象. ECMAScript 的 Boolean 值.数字和字符串的原始值的有趣之处在于它们是伪对象,这意味着 ...

  3. 利用Object.prototype.toString方法,实现比typeof更准确的type校验

    Object.prototype.toString方法返回对象的类型字符串,因此可以用来判断一个值的类型. 调用方法: Object.prototype.toString.call(value) 不同 ...

  4. 重写toString()方法来描述一个类

    package com.zch.test; /* toString方法以及重写toString方法 toString方法是一个自我描述方法 方法本身返回的是该对象的实现类的 类名 + @ + hash ...

  5. Object类的toString方法

          Object类是所有Java类的祖先.每个类都使用 Object 作为超类.所有对象(包括数组)都实现这个类的方法.在不明确给出超类的情况下,Java会自动把Object作为要定义类的超类 ...

  6. 100怎么变成100.00 || undefined在数字环境下是:NaN || null在数字环境下是0 || 数组的toString()方法把每个元素变成字符串,拼在一起以逗号隔开 || 空数组转换成字符串后是什么?

    100怎么变成100.00?

  7. js中的tostring()方法

    http://blog.sina.com.cn/s/blog_85c1dc100101bxgg.html js中的tostring()方法 (2013-11-12 11:07:43) 转载▼ 标签: ...

  8. JavaBean的toString方法工具类

    import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.r ...

  9. toString()方法

    前面的话 本文将介绍toString()方法,toString()方法返回反映这个对象的字符串 [1]undefined和null没有toString()方法 undefined.toString() ...

  10. 为何重写toString方法后会使哈希码能够打印出来

    首先还是推荐lz看源代码 简单的讲之所以调用了toString()方法,不是什么编译器默认的,而是因为lz你调用的是out.print()方法仔细看源代码,在PringStream类中,print方法 ...

随机推荐

  1. C++11的mutex和lock_guard,muduo的MutexLock 与MutexLockGuard

    互斥锁是用来保护一段临界区的,它可以保证某段时间内只有一个线程在执行一段代码或者访问某个资源. C++11的mutex和lock_guard C++11新增了mutex,使用方法和linux底下的常用 ...

  2. Unix的I/O模型

    对于一次I/O操作(以read为例),数据首先被拷贝到内核的某个缓冲区,然后再从内核缓冲区拷贝到应用进程缓冲区. 因此,一次I/O操作通常包含两个阶段: (1) 等待数据准备好 (2) 从内核向进程复 ...

  3. Spring官网阅读(十七)Spring中的数据校验

    文章目录 Java中的数据校验 Bean Validation(JSR 380) 使用示例 Spring对Bean Validation的支持 Spring中的Validator 接口定义 UML类图 ...

  4. gulp基本使用

    一.gulp是什么 gulp强调的是前端开发的工作流程,我们可以通过定义task事件定义事件的执行顺序,gulp去执行这些事件,构建整个前端开发的工作流程 gulp常见定义事件,例如: 变更静态资源 ...

  5. 08_CSS入门和高级技巧(6)

    排查错误 Chrome浏览器的审查功能. 错误1:选择器写错了,压根没有选择上: 如果写了一个错误的选择器, <style type="text/css"> dvi p ...

  6. 一文教你如何在ubuntu上快速搭建STM32 CubeIDE环境(图文超详细+文末有附件)

    在快速ubuntu上安装cubeide你值得拥有:适合对linux系统还不是很熟悉的同学: 文章目录 1 下载 cubeide 2 找到软件 3 安装 4 附件 5 总结 1 下载 cubeide 登 ...

  7. 解决anaconda与pycharm冲突导致import无法使用

    解决annacode与python冲突: 一.File->Setting-> 点击Add-> 然后就完美解决 二.记得重启,检查创建的项目是右键python package-> ...

  8. [hdu4609]计数方法,FFT

    题目:给一个数组a,从里面任选三个数,求以这三个数为三条边能构成三角形的概率. 思路:由于每个数只能用一次,所以考虑枚举三边中的最大边.先将a数组排序,然后枚举它的每个数x作为最大边,那么问题就是要求 ...

  9. android 数据库是否该关闭

    关于android多线程数据读写请看博客: android 多线程数据库读写 常常纠结于获取了SQLiteDatabase每次操作完是否要关闭的问题,每次关闭又怕影响性能,这里记录下SQLiteOpe ...

  10. spring junit--基础配置

    spring官方文档总提示要进行SpringJunit测试必须先配置两个信息: 1.使用Spring IOC功能配置 2.配置正确的JDBC或ORM框架连接数据库 下面进行spring3和hibern ...