考虑以下简化的接口继承层次结构:
// Starting point:
public interface Base {
void Foo();
}
public interface Derived extends Base {
}
它旨在将Foo
方法从Base
接口移动到Derived
接口:
// Desired end-point:
public interface Base {
}
public interface Derived extends Base {
void Foo();
}
为了逐步适应这种重大变化,需要在Base
一段时间内保持接口的向后兼容性。
这可以通过将Base
接口上的方法标记为@Deprecated
:
// Intermediate state:
public interface Base {
/**
* @deprecated This method is deprecated as of release X. Derived.Foo should be used instead.
*/
@Deprecated void Foo();
}
public interface Derived extends Base {
void Foo();
}
当我编译这段代码时,我收到一个编译器警告Derived
:
[弃用] 接口 Base 中的 Foo() 已弃用
奇怪的是,如果我@deprecated
从文档中删除Base
(但留下@Deprecated),这个警告就会消失。
我收到此警告是否正确,如果是,我该如何解决这个问题?
该警告似乎传达的Derived.Foo
是“使用” Base.Foo
(已弃用)。Derived.Foo
但是“使用”已弃用的唯一能力Base.Foo
是覆盖它。这似乎是说您不允许在派生方法中覆盖已弃用的接口方法。
如果是这种情况,我应该用装饰Derived
来@SuppressWarnings("deprecation")
抑制警告吗?