使用参数化类型来确定类型 [英] Using parameterized type to determine type

查看:82
本文介绍了使用参数化类型来确定类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读此链接

Java Generic Class - 确定类型



我试过这个程序。

  package my; 

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

public class GenericTypeIdentification {

public static void main(String args []){
Node< Integer> obj = new GenericTypeIdentification()。new SubNode< Integer>(1);
ParameterizedType parameterizedType =(ParameterizedType)obj.getClass()。getGenericSuperclass();
类型clazz = parameterizedType.getActualTypeArguments()[0];
if(clazz == Integer.class){
System.out.println(1);
}
else {
System.out.println(2);
}
}

class Node< T> {

私人最终T值;

public Node(T val){
this.value = val;
}

public T评估(){
返回值;
};

}

class SubNode< T>扩展节点< T> {

私人最终T值;

public SubNode(T val){
super(val);
value = val;
}

@Override
public T evaluate(){
返回值;
};






我的理解是它应该将原始输出作为 1 但打印 2 。请帮助我理解这一点。感谢。

解决方案

实际运作的技巧用于 google guice TypeLiteral 。在通用类的子类的构造函数中,即使在运行时,您也可以访问父类的通用实例化...因为泛型类型信息已在编译时保留用于继承目的。用法示例:

  TypeLiteral< MyClass> test = new TypeLiteral< MyClass>(){}; //注意建立一个匿名{}。子类
System.err.println(test.getType()); //输出MyClass

如果不使用generic的子类,由于类型擦除;对于大多数应用程序来说可能是矫枉过正的。


I was going throught this link

Java Generic Class - Determine Type

I tried with this program.

package my;

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

public class GenericTypeIdentification {

    public static void main( String args[] ) {
        Node<Integer> obj = new GenericTypeIdentification().new SubNode<Integer>( 1 );
        ParameterizedType parameterizedType = ( ParameterizedType ) obj.getClass().getGenericSuperclass();
        Type clazz = parameterizedType.getActualTypeArguments()[0];
        if ( clazz == Integer.class ) {
            System.out.println( 1 );
        }
        else {
            System.out.println( 2 );
        }
    }

    class Node<T> {

        private final T value;

        public Node( T val ) {
            this.value = val;
        }

        public T evaluate() {
            return value;
        };

    }

    class SubNode<T> extends Node<T> {

        private final T value;

        public SubNode( T val ) {
            super( val );
            value = val;
        }

        @Override
        public T evaluate() {
            return value;
        };
    }
}

My understanding was that it should printh output as 1 but it prints 2. Please help me in understanding this. Thanks.

解决方案

A trick that actually works is used in the google guice's TypeLiteral. In the constructor of a subclass of a generic class, you do have access to the parent's generic "instantiation", even at runtime... because the generic type information has been retained for inheritance purposes at compile-time. Example usage:

TypeLiteral<MyClass> test = new TypeLiteral<MyClass>() {}; // notice the {} to build an anon. subclass
System.err.println(test.getType()); // outputs "MyClass"

This does not work without using a subclass-of-a-generic, due to type erasure; and is probably overkill for most applications.

这篇关于使用参数化类型来确定类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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