【问题标题】:How can we create a class forName if the class is Generic type [duplicate]如果类是通用类型,我们如何创建一个类 forName [重复]
【发布时间】: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&lt;B&gt; 的实例吗?
  • 你有一个叫B的类吗?否则创建A&lt;B&gt; 没有多大意义......
  • 我已经编辑了第一篇文章,你能再看一遍吗?

标签: java class generics


【解决方案1】:

你不能因为通用信息在运行时被删除,那里只有 A 类。

但是,您可以创建一个单独的类,例如

public class M extends A<B> {
}

M 类将保存通用信息。

现在这里是关于如何从Class 对象中提取通用信息的完整演示:

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;

public class GenericInfoInClass {

    // class which contains generic information
    public static class StringList
            extends ArrayList<String>
            implements Comparable<StringList>
    {

        @Override
        public int compareTo(StringList o) {
            return 0;
        }
    }

    public static void main(String[] args) {
        Type[] genericInterfaces = StringList.class.getGenericInterfaces();
        for (Type genericInterface : genericInterfaces) {
            System.out.println(genericInterface);
            System.out.println("[Generic type arguments:]");

            ParameterizedType parameterizedType = (ParameterizedType) genericInterface;
            Type[] genericTypes = parameterizedType.getActualTypeArguments();
            for (Type genericType : genericTypes) {
                System.out.println("  -- " + genericType);
            }
        }

        System.out.println("----------------------------");

        Type genericSuperclass = StringList.class.getGenericSuperclass();
        System.out.println(genericSuperclass);
        System.out.println("[Generic type arguments:]");

        ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
        Type[] genericTypes = parameterizedType.getActualTypeArguments();
        for (Type genericType : genericTypes) {
            System.out.println("  -- " + genericType);
        }
    }
}

【讨论】:

  • 我更新了第一篇文章请再看一遍
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-05
  • 2021-05-22
  • 1970-01-01
  • 2018-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多