English 中文(简体)
Java。 我怎么能够预测哪类的用途?
原标题:Java. How can I upcast type use reflection?

我有两个班子:乌博伊和唐纳 Obj

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.

感谢。

问题回答

您实际上没有使用<代码>Class.getConstructor()和你希望通过的时间类型——该方法在形式参数之外运作。

如果你需要寻找与你的一些物体相匹配的构造者,那么你就必须在>>>>>>>>>>getConstructors(,检查每个构造者的正式参数,并检查你是否通过

(替代做法是重新思考你的思路,这样思考就没有必要了。)

没有标准的方法。 Try to google for some Solutions, e.g. http://www.xinotes.org/note/1329/。 如何利用思考获得参数类型?。 我记得春天和其他一些图书馆也有类似的利用职能。 你们是否使用春天?

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());

文件:Sugetperclass()





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签