安卓+到字符串
谁能告诉我 Android 中的 toString 是什么以及如何使用它?
作为例子将受到高度赞赏。
Can anybody please tell what toString in Android is for and how it can be used?
As example would be highly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
toString 不是特定于安卓。它是java的Object类中的一个方法,它是每个java对象的超类。 'toString' 用于返回对象的文本表示。这通常被 java 类覆盖以创建人类可读的字符串来表示该对象。
除了许多其他用途之外,它还广泛用于日志记录目的,以便以可读格式打印对象。附加带有字符串的对象会自动调用该对象的 toString(),例如
"abc" + myObject
将调用 myObject 的 'toString' 并将返回值附加到 "abc"toString 实现的一个很好的例子是看起来像 -
toString is not specific to android. Its a method in java's Object class, which is the superclass of every java object. 'toString' is meant for returning textual representation of an object. This is generally overridden by java classes to create a human readable string to represent that object.
Apart from many other uses, it is widely used for logging purpose so as to print the object in a readable format. Appending an object with string automatically calls the toString() of that object e.g.
"abc" + myObject
will invoke 'toString' of myObject and append the returned value to "abc"A good example of toString implementation would look like -
和普通Java中的一样...
我用它进行这样的调试:
使用
Log.d("TAG", myClassObject.toString());
我可以记录我的对象包含的内容...这只是无数可能性之一。
Its the same as in normal Java...
I use it for debugging like this:
with
Log.d("TAG", myClassObject.toString());
I can log what my object contains...thats just one of countless possibilities.
它不像Java。覆盖示例:
然后该方法的用法是
It's not like Java. Override example:
And then usage of the method is