【发布时间】:2018-03-13 06:29:03
【问题描述】:
Java 8 引入了允许描述方法主体的“默认方法”。
我想创建一个接口和两个子类。在接口 URL 中,我希望有 getURL() 方法:
public interface URL {
int getURL() {
return this.myURL;
} // obviously
}
我想在两个子类中定义 myURL 字段:
public class MyURL1 implements URL {
private String myURL = "http://test1.com";
}
public class MyURL2 implements URL {
private String myURL = "http://test2.com";
}
将由 getURL 返回。
在 Java 中可以吗?
【问题讨论】:
标签: java interface default-method