java 我如何声明实现类的java接口字段应该改进该字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6543243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:17:14  来源:igfitidea点击:

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

javainterface

提问by nguyên

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

我如何声明实现类应该细化该字段的java接口字段?

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.

谢谢你。

采纳答案by Ed Staub

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

参见规范的9.3 节。接口中没有覆盖字段 - 它们只是隐藏在某些上下文中,而在其他上下文中不明确。我宁愿远离。而是在接口中放置一个 getter (getEXAMPLE())

回答by Bohemian

You can't.

你不能。

Also, an interfacecan't require staticmethods to be defined on an implementation either.

此外,interface也不能要求static在实现上定义方法。

The best you can do is this:

你能做的最好的是:

public interface SomeInterface {
    public String getExample();
}