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

不羁的心 提交于 2019-11-29 14:12:52

You're looking for super.toString().

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.)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!