【发布时间】:2009-07-31 15:55:31
【问题描述】:
考虑以下简化的接口继承层次结构:
// 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 的编译器警告:
[deprecation] 接口 Base 中的 Foo() 已被弃用
奇怪的是,如果我从Base 的文档中删除@deprecated(但保留@Deprecated),此警告就会消失。
我收到此警告是否正确,如果是,我该如何解决?
警告似乎表明Derived.Foo 正在“使用”Base.Foo(已弃用)。但是,Derived.Foo 唯一能够“使用”已弃用的Base.Foo 的能力是覆盖它。这似乎是说您不允许在派生方法中覆盖已弃用的接口方法。
如果是这种情况,我应该用@SuppressWarnings("deprecation") 装饰Derived 以抑制警告吗?
【问题讨论】:
-
您使用的是哪个版本的 Java?我似乎记得这在某些时候被认为是一个错误,可能已修复......但我不能确定我是否正确地记住了这一点。我明白你的意思了。
标签: java interface overriding deprecated