tostring

Object类

孤者浪人 提交于 2020-01-01 17:24:27
Object Object类 Object类是java语言的根类,即所有类的父类,任何一个类都可以使用Object的方法 从这块儿我们可以看出来,直接打印对象的名字,就是打印他的toString方法 p = p.toString(); 我们把他返回成我们要的变量, 会发现输出打印的变成了我们重写后的。 实际过程中我们不用自己来重写,我们只需alt + insert ,点击toString即可 总结:看一个类是否重写toString方法,重写了直接打印这个对象即可 对于Object的equals方法,默认的比较的是两个对象的地址值,没有意义。 所以我们要重写equals方法,让他比较两个对象的属性 问题: 隐含着一个多态 多态的弊端:父类指向子类,我们无法看到子类特有的内容(属性和方法) 在这里我们使用对象的向下转型 Objects工具类 Objects是JDK1.7添加的一个类,提供了一些静态方法,用于计算对象的hashCode、返回对象的字符串表示形式、比较两个对象 可以防止空指针异常 return里面的意思是不是空才会调用这个equals方法,比较两个字符串的内容, 如果是空的话比较的是字符串的地址值。 来源: CSDN 作者: 黄大仙Ol 链接: https://blog.csdn.net/liuzf123/article/details/103792283

How does Object.toString() get the “memory address” and how can I mimic it

本小妞迷上赌 提交于 2020-01-01 08:58:41
问题 The toString method of Object is unique in that it seems to be the only place in Java where the memory address is viewable. How does Object do this? I would like to know so that I can mimic its implementation in a class of my own. I can't use super.toString() because I'm extending a class which overrides toString already. Update: The premise of my question asks for the memory address, but the answers have indicated that this premise is incorrect, so what I am actually asking is: How does

String.Format vs ToString()

好久不见. 提交于 2020-01-01 08:01:06
问题 Can anyone explain if there is any benefit in either one of the following methods: decimal d = 12.0m; // 1. how I'd have done it myLabel.Text = d.ToString(); // 2. how I saw someone do it today myLabel.Text = String.Format("{0}", d); Just to clarify, I'm not querying what the methods do, I'm obviously happy with that, just if there is perhaps a performance benefit in one over the other in this specific example. I'm aware of the added flexibility of cultures and formatting offered by string

String.Format vs ToString()

∥☆過路亽.° 提交于 2020-01-01 08:00:07
问题 Can anyone explain if there is any benefit in either one of the following methods: decimal d = 12.0m; // 1. how I'd have done it myLabel.Text = d.ToString(); // 2. how I saw someone do it today myLabel.Text = String.Format("{0}", d); Just to clarify, I'm not querying what the methods do, I'm obviously happy with that, just if there is perhaps a performance benefit in one over the other in this specific example. I'm aware of the added flexibility of cultures and formatting offered by string

C# ToString(“MM/dd/yy”) remove leading 0's [duplicate]

拥有回忆 提交于 2020-01-01 07:53:05
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Format .NET DateTime “Day” with no leading zero Is there a way to remove the leading zeros in the date format For example, X.ToString("MM/dd/yy") returns 07/02/11 but I'd like it to instead return 7/2/11. Is this possible? Thanks 回答1: X.ToString("M/d/yy") is what you need 回答2: Just remove the extra M and d . 回答3: You can use "M/d/yy" . See here for more details. 回答4: You can write X.ToString("M/d/yy") 回答5: It is

代码中代码块的认识

青春壹個敷衍的年華 提交于 2019-12-31 15:10:29
代码中代码块的认识 1.认识代码块 1.1. 字段的初始化方式 1》就地初始化 2》 使用构造方法初始化 3.》使用代码块初始化 1.2 什么是代码块 使用 {} 定义的一段代码 1.3 代码块分类 1.3.1 普通代码块:定义在方法中的代码块 1.3.2 构造代码块(也叫实例代码块) 与方法平齐,定义在类中的代码块(不加修饰符)。构造代码块一般用于初始化实例成员变量 但是不能初始化静态成员变量 1.3.3 静态代码块 与方法平齐,前用static修饰 静态代码块的优先级又高于实例代码块优先级 注意: 静态代码块不管生成多少个对象,其只会执行一次,且是最先执行的。 1.3.4. 代码块执行优先顺序 静态代码块>实例代码块>构造函数 2. toString方法 有些时候类通过一个方法向外展示太过麻烦,此时我们可以调用toString方法对外进行展示 如果不重写toString方法,会自动调用Object类的toString方法,会有如下结果 结果如下 生成了一个地址的哈希码,但是没有体现出对外数据的展示能力,我们用toString方法进行重写 我们仅仅添加了toString的重写方法,下来我们看看打印的结果是否达到预期目的 从上我们看到,通过toString方法的重写,我们完美的对外进行了展示,得到了预期目的 注意事项: toString 方法会在 println 的时候被自动调用.

Java: Getting the properties of a class to construct a string representation

一笑奈何 提交于 2019-12-31 09:21:19
问题 Let's say I have a class like this (and also further assume that all the private variables: public class Item { private String _id = null; private String _name = null; private String _description = null; ... } Now, if I want to build a toString() representation of this class, I would do something like this inside the Item class: @Override public String toString() { return (_id + " " + _name + " " + _description); } But what if I have say 15 private variables inside the class? Do I have to

.NET: How to convert Exception to string?

懵懂的女人 提交于 2019-12-31 08:08:10
问题 When an exception is thrown (while debugging in the IDE), i have the opportunity to view details of the exception: But in code if i call exception.ToString() i do not get to see those useful details: System.Data.SqlClient.SqlException (0x80131904): Could not find stored procedure 'FetchActiveUsers'. [...snip stack trace...] But Visual Studio has some magic where it can copy the exception to the clipboard : Which gives the useful details: System.Data.SqlClient.SqlException was unhandled by

What does passing an integer number as the argument for `toString` do?

久未见 提交于 2019-12-31 07:47:07
问题 I'm taking a course on JavaScript but have no guide on toString method, what's the purpose of these two outputs in JavaScript: (35).toString(36) >>> "z" !!! (35).toString(37) >>> throws a RangeError !!! I am utterly confused as to why I am getting these results, I would really appreciate it if someone could shed some light on this. 回答1: tldr: Number.prototype.toString can take an argument called radix that specifies the numeric base to use for the string representation of the number in

toString and valueOf truncates trailing 0s after decimal

梦想与她 提交于 2019-12-30 10:04:54
问题 In javascript, I've noticed that toString and valueOf truncates trailing 0s after a decimal. For example: var num = 0.00 var num2 = 0.0100 num.valueOf() or num.toString() // outputs 0 num2.valueOf() or num2.toString() // outputs 0.01 Is this normal behavior and is there someway to retain the trailing 0s? EDIT: I changed my original question because I realized after some testing that the above is root of the problem. Thanks. 回答1: It is not toString nor valueOf that truncates trailing 0s after