问题
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