爪哇。我如何使用反射向上转换类型?

发布于 2024-12-10 21:14:58 字数 1203 浏览 2 评论 0原文

我有两个类 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();
        }

    }

}

所以,我想在调用者构造函数中将我的子类型 DownObj 向上转换为父 UpObj 。 我认为通过帮助泛型这是可能的。像这样的东西。 任何人都知道这个具体如何使用。

谢谢。

I have a two classes UpObj and 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.
I think this is possible with help generics. Something like this .
Anybody know how exactly this use.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

〆凄凉。 2024-12-17 21:14:58

您实际上不应该将 Class.getConstructor() 与要传递给它的运行时类型一起使用 - 该方法根据形式参数进行工作。

如果您需要搜索与您拥有的某些对象匹配的构造函数,则必须循环 getConstructors(),检查每个构造函数的形参,检查是否找到了合适的使用 <代码>Class.isAssignableFrom()。我认为没有一种方便的方法可以让反射 API 为您执行重载解析。

(或者,重新考虑你的方法,这样就不再需要搞乱反射了。)

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.

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(). I don't think there's a convenient way to have the reflection API do overload resolution for you.

(Alternatively, rethink your approach so messing with reflection is no longer necessary.)

梦行七里 2024-12-17 21:14:58

没有标准的方法来做到这一点。尝试在谷歌上搜索一些解决方案,例如 http://www.xinotes.org/notes/note /1329/如何使用获取参数类型反射?。我记得 Spring 和其他一些库有类似的 util 函数来做到这一点。你用春天吗?

There is no standard way doing it. Try to google for some solutions, e.g. http://www.xinotes.org/notes/note/1329/ or How to get parameter types using reflection? . As I remember the Spring and some other libraries have similar util functions to do it. Do you use the Spring?

过去的过去 2024-12-17 21:14:58

我不太明白这个问题 - 通常不需要向上转换。
但问题似乎是获取对象的上层类,
如果使用 getSuperclass 方法:

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

文档:getSuperclass()

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:

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

Documentation: getSuperclass()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文