Java: design interface to force implementations to override toString

后端 未结 2 1950
遇见更好的自我
遇见更好的自我 2021-01-07 23:05

I\'m developing an SPI and would like to define a Reportable interface such that any implementations must override toString() to something that is

2条回答
  •  一生所求
    2021-01-07 23:41

    No, you can't do this. I'd suggest you choose a different method name, e.g.

    public interface Reportable
    {
        String createReport();
    }
    

    That will force implementations to write an appropriate method. toString() is already somewhat vague in its intention - whether it's for debug representations, user-visible representations (at which point you need to ask yourself about locales etc). Adding another meaning doesn't seem like a good idea to me.

提交回复
热议问题