【发布时间】:2021-06-02 16:16:21
【问题描述】:
class A<B> {
...
}
Class<A<B>> a = Class.forName(A.class.getTypeName()) throws error
a.getDeclaredConstructor(String.class).newInstance("hello world")
我应该怎么做才能为这个类创建一个实例?
我想知道如何使用泛型类型创建类名 这个例子不起作用
其他例子
public class SimpleTest {
interface TakePhoto<B> {
TakePhoto<B> clickShutter();
B done();
}
public @interface Specific {
String value();
}
@Specific("GALAXY-S5")
static class GalaxyS5TakePhoto<B> implements TakePhoto<B>{
B backPage;
GalaxyS5TakePhoto(B backPage){
this.backPage = backPage;
}
@Override
public TakePhoto<B> clickShutter() {
//...
return this;
}
@Override
public B done() {
return backPage;
}
}
@Test
public void Test() {
// if a Device is Galaxy S5, I want to make GalaxyS5TakePhoto
// if the device is other I will have other class models and make its instance
// I have package of these classes and want to make right instance.
// first collect specific classes which have TakePhoto
// then find and filter specific model
// then create new Instance
}
}
我只想用通用类型来上课
首先收集具有 TakePhoto 的特定类 然后找到并过滤特定型号 然后创建新实例
【问题讨论】:
-
你能说明如何在没有反射的情况下创建
A<B>的实例吗? -
你有一个叫
B的类吗?否则创建A<B>没有多大意义...... -
我已经编辑了第一篇文章,你能再看一遍吗?