我有以下类定义:

public class SyrianEdge extends BaseEdge<SyrianVertex, SyrianEdge>
                        implements Weighted, Blockable{
    ...
}


和界面:

public interface Weighted{
    public long getWeight();
}


叙利亚语边缘类没有方法public long getWeight(),但一切似乎都可以正常编译。

我想念什么?接口为什么不强制类具有其方法?

最佳答案

如果看这个基本示例:

interface Weighted{
    public long getWeight();
}

abstract class Parent implements Weighted {

    @Override
    public long getWeight() {
        return 0;
    }

}

class Child extends Parent {}


由于Parent类已经实现了此方法,因此Child类没有义务实现它。

10-07 13:13
查看更多