java 将对象转换为作为参数传递的类类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33565351/
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
Cast an object to class type passed as parameter
提问by hakuna matata
I have a parent class and 2 child classes. I am trying to implement a function that takes the type of the child and which child as parameters.
我有一个父类和 2 个子类。我正在尝试实现一个函数,该函数以孩子的类型和哪个孩子作为参数。
When I use child.newInstance()
, I want to store it in a variable of the type that is passed and call a function from the second parameter.
当我使用 时child.newInstance()
,我想将它存储在传递的类型的变量中,并从第二个参数调用一个函数。
Below are the classes
下面是课程
public class Parent {
public void test() {
System.out.println("Test from parent");
}
}
public class ChildA extends Parent {
public void testChildA() {
System.out.println("Test from child a");
}
}
public class ChildB extends Parent {
public void testChildB() {
System.out.println("Test from child b");
}
}
and here is the method I'm trying to implement
这是我正在尝试实施的方法
public class Driver {
Parent func(Class child, String whichChild) throws Exception {
// whichChild: "ChildA" or "ChildB"
Object obj = child.newInstance();
// cast obj to type of child and call the method "test" and "test" + whichChild
}
}
Can it be done what I am trying to do? If yes, how can I cast this object to the type that is passed?
可以做我想做的事吗?如果是,如何将此对象转换为传递的类型?
回答by lance-java
Not sure exactly what you're doing but you can use Class.cast(...)
.
不确定你在做什么,但你可以使用Class.cast(...)
.
Eg
例如
public <T> T getInstance(Class<T> type) {
Object o = type.newInstance();
T t = type.cast(o);
return t;
}
回答by Andy Turner
If you add a constraint to child
, you don't need a cast at all to get a Parent:
如果向 中添加约束child
,则根本不需要强制转换即可获得父级:
Parent func(Class<? extends Parent> child, String whichChild) throws Exception {
// whichChild: "ChildA" or "ChildB"
Parent obj = child.newInstance();
//...
}
However, you still can't call the testChildA
etc method, since all you have is an instance of Parent
. You'd need to use reflection to get the method:
但是,您仍然无法调用testChildA
etc 方法,因为您拥有的只是Parent
. 您需要使用反射来获取方法:
Method method = obj.getClass().getMethod().getMethod("test" + whichChild);
method.invoke(obj);
It would be better to have a method on the interface of Parent
which you can invoke, and is overridden in the subclasses.
最好在接口上有一个Parent
可以调用的方法,并且在子类中被覆盖。
public abstract class Parent {
public void test() {
System.out.println("Test from parent");
}
public abstract void testChild();
}
then simply call:
然后只需调用:
obj.testChild();
or, as Emanuele Ivaldi points out, just override test
in ChildA
and ChildB
and invoke that directly.
或者,如埃马努埃莱伊瓦尔迪所指出的,只是覆盖test
在ChildA
和ChildB
直接援引。
回答by Discern
Try this one.
试试这个。
public class Driver {
Parent func(Class child, String whichChild) throws Exception {
// whichChild: "ChildA" or "ChildB"
child.forName(whichChild);
Object obj = child.newInstance();
// cast obj to type of child and call the method "test" + whichChild
}
}
回答by Stultuske
I doubt it. As far as the compiler concerns, any instance of Class can be sent as parameter.
我对此表示怀疑。就编译器而言,任何 Class 实例都可以作为参数发送。
So, for the compiler, there is no proof that this instance is actually an instance of the type you send along.
因此,对于编译器,没有证据表明该实例实际上是您发送的类型的实例。