Override of toString() that makes use of the overridden toString()

前端 未结 2 1840
北荒
北荒 2020-12-20 22:11

Basically this is what I am trying to achieve.

classname@address(?)[original toString()], object\'s name, object\'s age

@Override public         


        
相关标签:
2条回答
  • 2020-12-20 22:46

    You're looking for super.toString().

    0 讨论(0)
  • 2020-12-20 22:48

    Actually, I was trying to achieve the same thing, where super.toString() wasn't exactly what I wanted. I believe that this is not possible.

    Specifically, I have a domain class in Groovy/Grails, and I wanted to override its toString() method to add some extra information:

    class Child extends Parent {
    
        public String toString() {
            return super.toString(this) + extra_info // not possible!!!
        }
    }
    

    This is not identical to return super.toString() + extra_info. In one case I get e.g.

    com.example.domain.Parent : 15, extra_info. (Wrong. The instance is the same, but toString includes type information.)

    In the other:

    com.example.domain.Child : 15, extra_info. (Correct, but not possible.)

    0 讨论(0)
提交回复
热议问题