• 泛型真实类型


    把项目中的代码抽出父类,有时需要用泛型。如果此时涉及到hibernate等数据库操作,比如"select u from User u",那么User怎么怎么表示出来呢?以下是研究的代码:

    父类:

    import static org.junit.Assert.*;
    import java.lang.reflect.ParameterizedType;
    import java.lang.reflect.Type;
    import org.junit.Test;
    
    public class Father<T> {
    
    	public Father() {
    		// TODO Auto-generated constructor stub
    		System.out.println(this.getClass());//class mypss.children
    		System.out.println(this.getClass().getSuperclass());//mypss.Father
    		System.out.println(this.getClass().getGenericSuperclass());//mypss.Father<java.lang.Long> 带上了泛型
    		ParameterizedType parameterizedType = (ParameterizedType) this.getClass().getGenericSuperclass();
    		Type[] actualTypes = parameterizedType.getActualTypeArguments();
    		System.out.println(actualTypes[0]);//class java.lang.Long 真实的泛型类型,Long
    		Class<T> actualType = (Class<T>) actualTypes[0];
    		System.out.println(actualType);
    	}
    	
    }
    

     子类:

    package mypss;
    
    import static org.junit.Assert.*;
    
    import org.junit.Test;
    
    public class children extends Father<Long> {
    
    	@Test
    	public void testde() throws Exception {
    		System.out.println("子类");
    	}
    }
    
  • 相关阅读:
    2014025640《嵌入式程序设计》第二周学习总结
    基于Struts2的SpringMVC入门
    2014025640《嵌入式设计》第一周学习总结
    Hadoop综合大作业
    hive基本操作与应用
    用mapreduce 处理气象数据集
    熟悉常用的HBase操作,编写MapReduce作业
    爬虫大作业
    熟悉常用的HDFS操作
    中文词频统计
  • 原文地址:https://www.cnblogs.com/yintingting/p/6872735.html
Copyright © 2020-2023  润新知