简体   繁体   English

Java。 如何上投类型使用反射?

[英]Java. How can I upcast type use reflection?

I have a two classes UpObj and DownObj 我有两个类UpObj和DownObj

public class UpObj {

    public UpObj() {
        System.out.println("Load UpObj ");
    }

}

public class DownObj extends UpObj {

    public DownObj() {
        System.out.println("Load DownObj ");
    }

}

public class Caller {

    UpObj obj;

    public Caller(UpObj obj) {
        this.obj = obj;
        System.out.println("!!!");
    }

}

public class GNUMakeFile {

    /**
     * @param args
     */
    public static void main(String[] args) {
        DownObj iView = new DownObj();
        Class<?> iViewClass = iView.getClass();
        Class<?> clazz;
        try {
            clazz = Class.forName("bla.bla.bla.Caller");
            Constructor<?> ctor = clazz.getDeclaredConstructor(iViewClass);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

    }

}

So, I want upcast my child type DownObj to parent UpObj in Caller constructor. 因此,我想在Caller构造函数中将我的孩子类型DownObj转换为父UpObj。 I think this is possible with help generics. 我认为使用帮助泛型是可能的。 Something like this . 像这样的东西。 Anybody know how exactly this use. 任何人都知道这种用法的确切含义。

Thanks. 谢谢。

There is no standard way doing it. 没有标准的方法。 Try to google for some solutions, eg http://www.xinotes.org/notes/note/1329/ or How to get parameter types using reflection? 尝试通过Google搜索一些解决方案,例如http://www.xinotes.org/notes/note/1329/如何使用反射获取参数类型? . As I remember the Spring and some other libraries have similar util functions to do it. 我记得Spring和其他一些库都具有类似的util函数。 Do you use the Spring? 你用弹簧吗?

You shouldn't really be using Class.getConstructor() with the runtime type of what you want to pass to it – the method works off the formal parameters. 您实际上不应该将Class.getConstructor()与要传递给它的运行时类型一起使用-该方法可以使用形式参数。

If you need to search for a constructor that matches some objects that you have, you'll have to loop over getConstructors() , inspect the formal parameters of each constructor, and check whether you found a suitable signature using Class.isAssignableFrom() . 如果需要搜索与您拥有的某些对象匹配的构造函数,则必须遍历getConstructors() ,检查每个构造函数的形式参数,并使用Class.isAssignableFrom()检查是否找到了合适的签名。 I don't think there's a convenient way to have the reflection API do overload resolution for you. 我认为没有一种简便的方法可以让反射API为您执行重载解析。

(Alternatively, rethink your approach so messing with reflection is no longer necessary.) (或者,重新考虑您的方法,因此不再需要将反射弄得一团糟。)

I didn't really understand the problem - upcasting is normally not needed. 我不是很了解这个问题-通常不需要向上转换。
but it seams like the problem is to get the upper class of the object, 但这似乎是问题在于获取对象的上层阶级,
if use the getSuperclass method: 如果使用getSuperclass方法:

    Constructor<?> ctor = clazz.getDeclaredConstructor(iViewClass.getSuperclass());

Documentation: getSuperclass() 文档: getSuperclass()

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

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