Home  >  Q&A  >  body text

java - 关于重写toString()方法

想请教各路大神~~
如[id=" + id + "]是什么意思呢?/(ㄒoㄒ)/~~
求解答~/(ㄒoㄒ)/~~

PHP中文网PHP中文网2716 days ago853

reply all(3)I'll reply

  • 黄舟

    黄舟2017-04-18 09:35:51

    toString, as the name suggests, represents this variable in the form of a string. It can also be understood that this variable/class/instance is expressed as XXXX in a string. As for whether this string is unique, that is another matter.

    Obviously, the toString method here just uses string concatenation to express its attributes, for example:

    println(this.toString())

    The output is similar to: Goddess [id=1 , user_name ="aaaa", sex="man".........]

    The value obtained by toString is generally not for machines to see, but for people to see. In other words, in the toString method, we must find ways to let people see clearly at a glance what variable it is and what its value is, or the function of this class can be seen at a glance.

    [id=" + id + "] is very simple. The output result is a string similar to [id=" + id + "]很简单,输出结果就是类似Goddess [id=1 , user_name ="aaaa", sex="man".........], which expresses that the name of this class (this variable) is Goddess, and it has XX attributes. The value of the attribute id is 1, the value of sex is man.

    reply
    0
  • 黄舟

    黄舟2017-04-18 09:35:51

    This is using the plus sign to concatenate strings. [id=" + id + "]It indicates the value of the id. The advantage of this is that it will look more friendly when printed to the console.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 09:35:51

    Is it because I don’t understand string concatenation and regard it as the assignment operation of id?

    [id=" + id + "

    The first quote above is the quote at the end of the previous string, and the second is the quote at the beginning of the next string:

    "...[id=" //第一个字符串
    + id + //拼接的id值,自动将数值等类型转换为字符串
    "..." //第二个字符串
    

    reply
    0
  • Cancelreply