什么是Java和.NET原原因的ToString()? [英] What are the original reasons for ToString() in Java and .NET?

查看:223
本文介绍了什么是Java和.NET原原因的ToString()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用的ToString()谦虚在过去,我发现它在许多情况下非常有用的。但是,我使用这种方法将很难有理由把这种方法莫属 System.Object的。我野生的猜测是,在工作期间的某个时刻进行,并举行拿出了.NET框架的初步设计会议,会议决定,这是必要的 - 或者至少是非常有用的 - 包含一个的ToString(),将通过一切在.NET框架中实现的方法。

I've used ToString() modestly in the past and I've found it very useful in many circumstances. However, my usage of this method would hardly justify to put this method in none other than System.Object. My wild guess is that, at some point during the work carried out and meetings held to come up with the initial design of the .NET framework, it was decided that it was necessary - or at least extremely useful - to include a ToString() method that would be implemented by everything in the .NET framework.

有谁知道确切的原因是?我失去了一吨的情况下,的ToString()足以证明有用的是 System.Object的的一部分吗?什么是原来的原因的ToString()

Does anyone know what the exact reasons were? Am I missing a ton of situations where ToString() proves useful enough as to be part of System.Object? What were the original reasons for ToString()?

非常感谢!

PS - 还是那句话:我不是质疑方法或暗示它是没有用的,我只是好奇,想知道是什么使得它如此有用,需要放在 System.Object的

PS - Again: I'm not questioning the method or implying that it's not useful, I'm just curious to know what makes it SO useful as to be placed in System.Object.

附注 - 想象一下:

AnyDotNetNativeClass someInitialObject =新AnyDotNetNativeClass([某些构造函数参数]);

AnyDotNetNativeClass initialObjectFullCopy = AnyDotNetNativeClass.FromString(someInitialObject.ToString());

这是不是很酷?

编辑(1):

(一) - 基于一些答案,似乎.NET语言继承了这个从Java。所以,我添加的Java的主题,并以标签为好。如果有人知道为什么这是用Java实现的原因,那么请一些线索!

(A) - Based on some answers, it seems that .NET languages inherited this from Java. So, I'm adding "Java" to the subject and to the tags as well. If someone knows the reasons why this was implemented in Java then please shed some light!

(二) - 静态假设 FromString VS序列化:肯定的,但是这是一个完全不同的故事,权

(B) - Static hypothetical FromString vs Serialization: sure, but that's quite a different story, right?

推荐答案

据初步加入到对象的调试和日志记录。如果你的JavaDoc的Object.toString你可以推断出这个(<一href="http://java.sun.com/javase/6/docs/api/java/lang/Object.html#toString%28%29">http://java.sun.com/javase/6/docs/api/java/lang/Object.html#toString()),因为它输出的类名,其次为@,随后的对象的哈希code中的无符号的十六进制再presentation,唯一的地方,我可以看到这是非常有用的日志或控制台。

It was initially added to Object for debugging and logging purposes. You can deduce this if you look at the JavaDoc for Object.toString (http://java.sun.com/javase/6/docs/api/java/lang/Object.html#toString()), as it outputs the classname, followed by @, followed by the unsigned hexadecimal representation of the hash code of the object. The only place I can see this being very useful is in a log or console.

但Java的创造者故意留下这个方法无终,这样子类可以(也应该)忽略它,而不是输出个子类的具体信息。他们可能刚刚实施的JVM,这样传递一个对象到,需要一个字符串的方法,它会产生上述的哈希值,并将其传递给方法,而是他们是好的,实现了它作为一种方法,你会这么方便覆盖。

But the Java creators intentionally left this method non-final so subclasses could (and should) override it to instead output more subclass-specific information. They could have just implemented the JVM such that passing an object into any method that required a string, it would generate that hash value above and pass it into the method, but instead they were nice and implemented it as a method that you could so conveniently override.

和其在目标水平,使他们可以安全地假定实施任何对象可以被写入到一个日志/控制台。它是一种方便的假设,在Java语言中。

And its implemented at the Object level so they you can safely assume any object can be written out to a log/console. Its a convenient assumption in the Java language.

这篇关于什么是Java和.NET原原因的ToString()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆