简体   繁体   English

运行时Java类扩展不起作用

[英]Runtime Java Class extension not working

Good day, I have the following problem: class B extends class A and methods of both are called by another method in another class after instantiating class B (example follows): 美好的一天,我遇到以下问题:类B扩展了类A,并且实例化类B后,另一个类中的另一个方法调用了这两种方法:

public class A{
    //fields
    //constructors
    //methods
}

public class B extends A{
    //fields
    //constructors
    //methods
}

public class CALLER{

    public A getA(enum E){
        return Factory.getB(otherobject,E);
    }
}

public class Factory{
    public static B getB(object o,enum e){
         //do something with enums and get B
         b = new B();
         //populate b
         return b;
    }
}

Class B does not override any method of class A. Somehow at compile time this doesn't get any error but at runtime class CALLER excepts: java.lang.NoSuchMethodError: Factory.getB(object,enum) A 类B不会覆盖类A的任何方法。在编译时,这不会产生任何错误,但是在运行时类CALLER除外:java.lang.NoSuchMethodError:Factory.getB(object,enum)A

My question is: if B extends A why a method from a different class can't return A even if its return clause returns a B object directly? 我的问题是:如果B扩展了A,即使其他类的方法的return子句直接返回B对象,为什么它也不能返回A? In fact changing: 实际上正在改变:

public static B getB(object, enum);

with

public static A getB(object, enum);

solves the exception but then I get another exception (classCast) because obviously in other parts of the code it is awaiting a B type object, not an A. 解决了异常,但随后又得到另一个异常(classCast),因为显然在代码的其他部分,它正在等待B类型对象,而不是A。

Thanks in advance. 提前致谢。

You would get this exception if you had compiled CALLER.java with another version of Factory.java that would have getB returning A, then updated Factory.java so that getB returns B, then recompiled Factory.java but not CALLER.java 如果您用另一个版本的Factory.java编译了CALLER.java,则该异常将使getB返回A,然后更新Factory.java,以便getB返回B,然后重新编译Factory.java,但不重新编译CALLER.java,就会出现此异常。

UPDATE: 更新:

Perhaps you want to do something like this: 也许您想做这样的事情:

public abstract class Factory {
    public abstract A getInstance(object o, enum e);
}

public class FactoryB extends Factory {
    @Override
    public B getInstance(object o,enum e){
         //do something with enums and get B
         b = new B();
         //populate b
         return b;
    }
}

But the factory would then need to be instanciated. 但是这时需要实例化工厂。

The first one looks like a reflection error. 第一个看起来像是反射错误。 The java reflection classes look for the exact method signature "A getB(Object,Enum)" and not "B getB(Object,Enum)". Java反射类将查找确切的方法签名“ A getB(Object,Enum)”,而不是“ B getB(Object,Enum)”。
The second, as long as you actually create an object of type B in your getB(..) method, it will return this object. 第二,只要您实际上在getB(..)方法中创建类型B的对象,它将返回该对象。 The classCast exception will only be thrown if you create a new A instead of a new B. 仅当您创建一个新的A而不是一个新的B时,才会引发classCast异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM