当包含参数化类型时,在Java中创建泛型类型的实例吗? [英] Create instance of generic type in Java when parameterized type is contained?

查看:175
本文介绍了当包含参数化类型时,在Java中创建泛型类型的实例吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题的后续行动:

This is a follow-up to my question:

创建的实例当参数化类型通过层次结构传递时,Java中的泛型类型?

为了尝试从包含的类创建新的泛型,我尝试采用Steve B的创建匿名子类的方法:

For attempting to create a new generic from a contained class, I tried to adapt Steve B's approach of creating an anonymous subclass:

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class ParameterizedTypeEg {
    ParameterizedTypeEg () {
        ContainsParameterized<String> containString = new ContainsParameterized<String>();
    }
    public class Parameterized<E> {
        Parameterized () {
        }
        public Class<E> getTypeParameterClass() {
            Type type = getClass().getGenericSuperclass();
            ParameterizedType paramType = (ParameterizedType) type;
            return (Class<E>) paramType.getActualTypeArguments()[0];
        }
        public Constructor<E> getTypeParameterConstructor() {
            Constructor<E> constructor = null;
            try {
                constructor = getTypeParameterClass().getConstructor(QueriedColor.class);
            } catch (NoSuchMethodException e) { System.err.println(e); }
            return constructor;
        }           
    }
    class ContainsParameterized<E> {
        ContainsParameterized () {
            Parameterized<E> contained = new Parameterized<E>(){};
            try {
                E element = contained.getTypeParameterConstructor().newInstance();
            }
            catch (InstantiationException e) { System.err.println(e); }
            catch (IllegalAccessException e) { System.err.println(e); }
            catch (InvocationTargetException e) { System.err.println(e); }
        }
    }
    public static void main(String[] args) {
        new ParameterizedTypeEg();
    }
}

请注意该行 参数化包含= new Parameterized(){};

Please note the line Parameterized contained = new Parameterized(){};

在这里,我正在尝试创建匿名子类,如另一篇文章中的Steve B所建议的那样.但是,我在getTypeParameterClass()方法中收到ClassCastException.这与我的其他帖子中的异常类型相同.那使我认为我可以使用与史蒂夫·B建议的相同的解决方案.

Here I am attempting to create the anonymous subclass, as suggested by Steve B in the other post. However, I get a ClassCastException in the getTypeParameterClass() method. This is the same type of exception as in my other posting. That lead me to think that I could use the same solution as Steve B suggested for that problem.

推荐答案

只要在某类型的类型定义中对E进行了参数化,您要尝试执行的操作就可以起作用.例如:

What you're trying to do can work so long as E is parameterized in a type definition somewhere. For example:

Parameterized<E> pe = new Parameterized<E>();

这将不允许您解析E,因为它不是类型定义的一部分.另一方面,这是

This will not allow you to resolve E since it's not part of a type definition. On the otherhand, this:

class StringE extends Parameterized<String> {}

或者这个:

Parameterized<String> ps = new Parameterized<String>(){};

将起作用,因为我们将E的值指定为类型定义的一部分.要解析E的值,您可以使用 TypeTools :

will work since we're specifying the value of E as part of a type definition. To resolve the value of E, you might use TypeTools:

Class<?> stringType = TypeResolver.resolveRawArgument(Parameterized.class, ps.getClass());
assert stringType == String.class;

这篇关于当包含参数化类型时,在Java中创建泛型类型的实例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆