Android + toString [英] Android + toString

查看:17
本文介绍了Android + toString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我 Android 中的 toString 是做什么用的以及它是如何使用的?

Can anybody please tell what toString in Android is for and how it can be used?

作为例子将不胜感激.

推荐答案

toString 并非特定于 android.它是java的Object类中的一个方法,它是每个java对象的超类.'toString' 用于返回对象的文本表示.这通常被 java 类覆盖以创建一个人类可读的字符串来表示该对象.

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.

除了许多其他用途外,它被广泛用于记录目的,以便以可读格式打印对象.使用字符串附加对象会自动调用该对象的 toString(),例如"abc" + myObject 将调用 myObject 的 'toString' 并将返回值附加到 "abc"

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"

toString 实现的一个很好的例子看起来像 -

A good example of toString implementation would look like -

  @Override
  public String toString() {
    return new StringBuilder()
    .append("{Address:")
    .append(" street=").append(street)
    .append(", pincode=").append(pincode)
    .append("}").toString();
  }

这篇关于Android + toString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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