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. python基础入门教程(一条龙服务)

    一.语言基础 01-1 计算机系统 解释器下载 变量   小整数池 01-2 垃圾回收机制 02 数据类型 运算符(解压赋值等) 03 流程控制 if while for 04 整形 字符串 列表 0 ...

  2. LeetCode 45. 跳跃游戏 II | Python

    45. 跳跃游戏 II 题目来源:https://leetcode-cn.com/problems/jump-game-ii 题目 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素 ...

  3. Programming Languages_05 FWAE

    FWAE : Concrete syntax <FWAE> ::= <num> | {+ <FWAE> <FWAE>} | {- <FWAE> ...

  4. spring的单元测试

    如果spring 4.3.18这个版本的spring要使用junit,需要使用junit的junit-4.12之上的版本.使用这个版本junit的时 候需要引入hamcrest-all的jar包.之前 ...

  5. oracle常用字符函数

    字符函数: concat:(字符连接函数) --字符连接 select concat('con','cat') from dual; select 'co'||'nc'||'at' from dual ...

  6. Mahout聚类和kafaka相关知识

    1.说几种距离测度Mahout: 欧式距离测度:平方欧式距离测度:曼哈顿距离测度:余弦距离测度:加权距离测度 2.K-means算法参数:

  7. uniapp自定义简单弹窗组件

    2.0(2019-08-31) 船新版本的信息弹窗组件 插件市场地址:http://ext.dcloud.net.cn/plugin?id=672 可以弹出很多条信息,并单独控制消失时间.点击消失. ...

  8. Python-给数字/字符串前加0

    zfill方法用来给字符串前面补0

  9. qt绘制甘特图

    重写paintEvent事件,代码如下 void xx::paintEvent(QPaintEvent *event){ QPainter painter(this); //绘制x,y轴,_maxWi ...

  10. jvm入门及理解(五)——运行时数据区(虚拟机栈)和本地方法接口

    一.虚拟机栈背景 由于跨平台性的设计,java的指令都是根据栈来设计的.不同平台CPU架构不同,所以不能设计为基于寄存器的. 优点是跨平台,指令集小,编译器容易实现,缺点是性能下降,实现同样的功能需要 ...