toString()方法的使用
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()方法的使用的更多相关文章
- 采用重写tostring方法使ComboBox显示对象属性
当ComboBox中添加的是对象集合的时候,如果运行就会发现显示是的命令空间.类名,而如果我们想显示对象属性名的时候,我们就可以在对象类中重写object基类中的tostring方法.
- ECMAScript toString() 方法
ECMAScript 定义所有对象都有 toString() 方法,无论它是伪对象,还是真对象. ECMAScript 的 Boolean 值.数字和字符串的原始值的有趣之处在于它们是伪对象,这意味着 ...
- 利用Object.prototype.toString方法,实现比typeof更准确的type校验
Object.prototype.toString方法返回对象的类型字符串,因此可以用来判断一个值的类型. 调用方法: Object.prototype.toString.call(value) 不同 ...
- 重写toString()方法来描述一个类
package com.zch.test; /* toString方法以及重写toString方法 toString方法是一个自我描述方法 方法本身返回的是该对象的实现类的 类名 + @ + hash ...
- Object类的toString方法
Object类是所有Java类的祖先.每个类都使用 Object 作为超类.所有对象(包括数组)都实现这个类的方法.在不明确给出超类的情况下,Java会自动把Object作为要定义类的超类 ...
- 100怎么变成100.00 || undefined在数字环境下是:NaN || null在数字环境下是0 || 数组的toString()方法把每个元素变成字符串,拼在一起以逗号隔开 || 空数组转换成字符串后是什么?
100怎么变成100.00?
- js中的tostring()方法
http://blog.sina.com.cn/s/blog_85c1dc100101bxgg.html js中的tostring()方法 (2013-11-12 11:07:43) 转载▼ 标签: ...
- JavaBean的toString方法工具类
import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.r ...
- toString()方法
前面的话 本文将介绍toString()方法,toString()方法返回反映这个对象的字符串 [1]undefined和null没有toString()方法 undefined.toString() ...
- 为何重写toString方法后会使哈希码能够打印出来
首先还是推荐lz看源代码 简单的讲之所以调用了toString()方法,不是什么编译器默认的,而是因为lz你调用的是out.print()方法仔细看源代码,在PringStream类中,print方法 ...
随机推荐
- A. Guest From the Past(数学推式子)
\(n元,买塑料杯子a元,买玻璃杯子b元,但玻璃杯子用完后可以卖c元\) \(求最多买的杯子.\) \(---------------------------分割------------------- ...
- 一看就懂的Ubuntu系统下samba服务器安装配置教程
文章目录 前言 环境搭建 安装 配置 Examples 1 创建共享(任何人都可以访问) 2 单用户权限(需要密码访问) 添加samba用户 配置参数 3 支持游客访问(单用户拥有管理员权限) 前言 ...
- Battery Charging Specification Revision 1.2 中文版本
Battery Charging Specification Revision 1.2 Li,Guanglei 2014.04.03 Rev0.1 转载请注明转自:http://blog.csdn.n ...
- Flutter中如何使用WillPopScope
老孟导读:在Flutter中如何实现点击2次Back按钮退出App,如何实现App中多个Route(路由),如何实现Back按钮只退出指定页面,此篇文章将告诉你. WillPopScope WillP ...
- [hdu5418 Victor and World]floyd + 状压DP 或 SPFA
题意:给n个点,m条边,每次只能沿边走,花费为边权值,求从1出发经过所有其它点≥1次最后回到1的最小花费. 思路: 状压DP.先用Floyd得到任意两点间的最短距离,转移时沿两个点的最短路转移.此时的 ...
- wangeditor在移动端的web应用
废话不多说,直接上代码 前端(前端多说一句,在初始使用阶段,不知道是怎么回事,复制在看云上的文档的配置参数时,一直有错误,后台获取不到$_file,整整一上午,下午上网搜了一下别人的上传图片代码才好用 ...
- GitHub使用SSH连接以及生成修改添加密钥详细过程
目录 1. 先看看本地有没有SSH密钥 2. 生成/修改密钥 3. 把SSH密钥添加到ssh-agent 4. 把SSH密钥添加到GitHub账户里 5. 测试使用ssh地址clone仓库 6. 把远 ...
- Django数据库表初始化缓存清除
新建的django项目中没有应用app01??? models中也没有UserInfo表???? 但在migrate是却一直报错!!!!! 产生此种现象的原因: 之前的项目中肯定是用到过应用app01 ...
- python之pytest框架实现
一.pytest测试框架简介: pytest是一个非常成熟的全功能的Python测试框架,主要有以下几个特点: 简单灵活,容易上手 支持参数化 能够支持简单的单元测试和复杂的功能测试,还可以用来做se ...
- vue修改对象的属性值后页面不重新渲染
原文地址:vue修改对象的属性值后页面不重新渲染 最近项目在使用vue,遇到几次修改了对象的属性后,页面并不重新渲染,场景如下: HTML页面如下: [html] view plain copy &l ...