java Java反射:动态创建类实例并将其分配给父对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6619930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:39:05  来源:igfitidea点击:

Java Reflection: Creating class instance dynamically and assigning it to Parent object

javareflection

提问by Kazoom

String child = "C";
Parent p = null;
try {
    Class c1 = new Class.forName(child);
    Constructor co = c1.getConstructor();
    // p=co.newInstance(null); //This gives compilatoin error cannot
    // conver object to Parent
    // p=(c1.getClass())co.newInstance(null);//also gives cast errror
    p = (Parent) co.newInstance(null);// this works but it typecasts the
    // child to Parent
} catch (Exception e) {
}

What am i trying to do.

我想做什么。

I have multiple Child classes inherited from Parent. I am getting child class name as string input.

我有多个继承自 Parent 的 Child 类。我正在获取子类名称作为字符串输入。

I want to instantiate the object of Child class and assign it to Parent. I do not want to type cast Child to Parent. As later in the code i need to compare two Child classes. If I typecast it to Parent. I cannot differentiate between Child1 and Child2.

我想实例化 Child 类的对象并将其分配给 Parent。我不想将 Child 类型转换为 Parent。在后面的代码中,我需要比较两个 Child 类。如果我将它类型转换为父级。我无法区分 Child1 和 Child2。

回答by Mark Peters

I don't think you understand what "typecasting" does. It has absolutely no effecton the object itself. Using p = (Parent) tsimply does a runtime check on tto make sure that the type of tis assignable to Parent(i.e. either tis-a Parentor it is-a subclass of Parent) . Afterward, twill still be a Child1or whatever its actualtype always has been.

我认为您不了解“类型转换”的作用。它对对象本身完全没有影响。Usingp = (Parent) t只是执行运行时检查t以确保 的类型t可分配给Parent(即tis-aParent或它是 的子类Parent)。之后,t仍将是 aChild1或其实际类型一直是。

Use the explicit cast.

使用显式转换。

回答by aroth

You might try something like:

您可以尝试以下操作:

Object parent = null;
String child = String.class.getName();  //not necessary, can just use String.class directly
Class childClass = Class.forName(child);
Class parentClass = Object.class;

if (parentClass.isAssignableFrom(childClass)) {
    parent = childClass.newInstance();
}

System.out.println("Parent is:  " + parent);

回答by Charlee Chitsuk

Even the Parent is cast form the 2 different children as

甚至 Parent 也是从 2 个不同的孩子中投射出来的

Parent parent1 = (Parent)child1;
Parent parent2 = (Parent)child2;

The parent1 and parent2 are totally different based on each child.

parent1 和 parent2 根据每个孩子是完全不同的。

You may see the difference by printing them as

您可以通过将它们打印为

System.out.println(parent1.getClass().getName());
System.out.println(parent2.getClass().getName());

Then you may compare it by using the getName() as well.

然后您也可以使用 getName() 进行比较。

I hope this may help to achieve the requirement.

我希望这可能有助于达到要求。

Regards,

问候,

Charlee Ch.

查理·Ch。