【发布时间】:2013-10-31 15:14:48
【问题描述】:
抱歉标题含糊;我不完全确定问题出在哪里。
背景
简而言之:某个基类的子类必须定义 3个特定的静态方法,隐藏基类的静态方法。实现描述类在初始化时检查这一点。然而,在运行应用程序时似乎是随机的,但是在初始化期间我得到运行时异常,说我没有正确地重新实现这些方法。但是,当这种情况发生时,我正在其他地方从事不相关的课程,这种情况很少发生,并且只需打乱方法的顺序即可将其修复很长时间。
代码
所以,三个类:Base、Derived 和 AlgImplementation 类:
AlgImplementation 构造函数:
/* verifying that the extending class has implemented these methods */
if (this.getAlgorithmClassName() == null) {
throw new IllegalArgumentException("The Algorithm Class of this algorithm has not re-implemented the getAlgorithmClassName() method from as specified.");
}
if (this.getAlgorithmClassDescription() == null) {
throw new IllegalArgumentException("The Algorithm Class of this algorithm has not re-implemented the getAlgorithmClassDescription() method from as specified.");
}
if (this.getAlgorithmClassAnalyticLevel() == null) {
throw new IllegalArgumentException("The Algorithm Class of this algorithm has not re-implemented the getAlgorithmClassAnalyticLevel() method from as specified.");
}
这就是问题发生的地方,其中一项检查失败了。我从上述一项或多项中得到 IllegalArgumentException。我可以简单地在派生类中移动实现的顺序,强制重新构建该代码,然后它就可以正常工作了。
基类和派生类都具有相同的简单静态方法,但它们返回的静态字段的定义不同:
class Derived extends Base {
public static AnalyticLevel getAlgorithmClassAnalyticLevel()
{
return ANALYTIC_LEVEL;
}
public static String getAlgorithmClassName()
{
return NAME;
}
public static String getAlgorithmClassDescription()
{
return DESCRIPTION;
}
}
以上字段都是非空的静态final Strings。
现在,在 Derived 类中,我声明了一个静态最终 AlgImplementation 字段: public static final AlgImplementation derivedAlg = new AlgImplementation("xyz", Derived.class, "我们的 XYZ 派生类", "");
最后,我认为您需要知道的最后一件事是,此 AlgImplementation 实例为每个静态方法类执行此操作:
public String getAlgorithmClassName() {
String className = "";
try {
className = (String)algorithmClass.getDeclaredMethod("getAlgorithmClassName", new Class<?>[0]).invoke(null, new Object[0]);
} catch (Exception e) {
throw new UnsupportedOperationException("Required static method getAlgorithmClassName() not implemented on "+this.getClass().getName());
}
return className;
}
终于
所以,我的问题是:如果实际上声明了这些方法,那么对派生方法的检查怎么会失败?声明一个引用它所定义的类的静态 AlgImplementation 字段是否存在问题(导致一些奇怪的编译顺序或类似问题)?
错误是在 Derived 类的初始化过程中,特别是在初始化静态 AlgImplementation 字段的那一行,这就是为什么我认为在 Derived 类本身中这样做可能存在问题。
【问题讨论】:
-
您不能派生、重载或覆盖静态方法。您通常通过类限定符访问它们
-
我用错了词,编辑为“定义”而不是“覆盖”。派生类隐藏基类方法。
-
您得到的是哪个错误,IllegalArgumentException 还是 UnsupportedOperationException?
-
IllegalArgumentException
标签: java inheritance dynamic static