how can I declare java interface field that implement class should refine that field

自古美人都是妖i 提交于 2019-12-23 07:41:36

问题


How can I declare java interface field that implement class should refine that field ?

for example

public interface IWorkflow{
    public static final String EXAMPLE;// interface field 
    public void reject();
}

// and implement class
public class AbstWorkflow implements IWorkflow
{
    public static final String EXAMPLE = "ABCD"; /*MUST HAVE*/
    public void reject(){}
...
}

Thank you.


回答1:


See section 9.3 of the specification. There is no overriding of fields in interfaces - they are just hidden in some contexts, and ambiguous in others. I'd just stay away. Instead put a getter in the interface (getEXAMPLE())




回答2:


You can't.

Also, an interface can't require static methods to be defined on an implementation either.

The best you can do is this:

public interface SomeInterface {
    public String getExample();
}


来源:https://stackoverflow.com/questions/6543243/how-can-i-declare-java-interface-field-that-implement-class-should-refine-that-f

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