zoukankan      html  css  js  c++  java
  • java getCanonicalName getSimpleName和getName的区别和用法

    接口:

    Java代码  收藏代码
    1. package com.test;  
    2.   
    3. public interface Fruit {  
    4.   
    5. }  

     

     

    一个实现类:

    Java代码  收藏代码
    1. package com.test;  
    2.   
    3. public class Apple implements Fruit {  
    4.   
    5. }  

     

    基本测试类:

    Java代码  收藏代码
    1. package com.test;  
    2.   
    3. import java.util.ArrayList;  
    4. import java.util.List;  
    5.   
    6. public class TestName {  
    7.     public static void main(String[] args) {  
    8.         Fruit apple=new Apple();  
    9.         System.out.println(apple.getClass().getCanonicalName());//返回com.test.Apple  
    10.         System.out.println(apple.getClass().getSimpleName());//Apple  
    11.         System.out.println(apple.getClass().getName());//返回com.test.Apple  
    12.           
    13.         Apple[] arrApple=new Apple[]{};  
    14.         System.out.println(arrApple.getClass().getCanonicalName());//返回com.test.Apple[]  
    15.         System.out.println(arrApple.getClass().getSimpleName());//返回Apple[]  
    16.         System.out.println(arrApple.getClass().getName());//返回[Lcom.test.Apple;  
    17.           
    18.         System.out.println(String.class.getCanonicalName());//返回java.lang.String  
    19.         System.out.println(String.class.getSimpleName());//返回String  
    20.         System.out.println(String.class.getName());//返回java.lang.String  
    21.           
    22.         System.out.println(int.class.getCanonicalName());//返回int  
    23.         System.out.println(int.class.getSimpleName());//返回int  
    24.         System.out.println(int.class.getName());//返回int  
    25.           
    26.         Apple a1=new Apple();  
    27.         Apple a2=new Apple();  
    28.         List<Apple> appleList=new ArrayList<Apple>();  
    29.         appleList.add(a1);  
    30.         appleList.add(a2);  
    31.         System.out.println(appleList.getClass().getCanonicalName());//返回java.util.ArrayList  
    32.         System.out.println(appleList.getClass().getSimpleName());//返回ArrayList  
    33.         System.out.println(appleList.getClass().getName());//返回java.util.ArrayList  
    34.           
    35.     }  
    36. }  

     

     

    实际应用: hql的泛型查询

     

    Java代码  收藏代码
    1. public <T> List<T> getRecords(Class<T> c,Date startDate,Date endDate){  
    2.         StringBuilder hql = new StringBuilder("select t from ");  
    3.         hql.append(c.getCanonicalName());  
    4.         hql.append(" t where t.statTime>=:startTime and t.statTime<:endTime ");  
    5.   
    6.         Query query = sessionFactory.getCurrentSession().createQuery(hql.toString());  
    7.         query.setParameter("startTime", startDate);  
    8.         query.setParameter("endTime", endDate);  
    9.           
    10.         return query.list();  
    11.     }  
    12. }  
  • 相关阅读:
    Asp.NET调用有道翻译API
    JSON C# Class Generator ---由json字符串生成C#实体类的工具
    让jQuery的contains方法不区分大小写
    javascript parseUrl函数(来自国外的获取网址url参数)
    typescript
    webpack 第二部分
    express node 框架介绍
    webpack 最新版
    es6 字符串 对象 拓展 及 less 的语法
    es6 的数组的方法
  • 原文地址:https://www.cnblogs.com/lechance/p/4373183.html
Copyright © 2011-2022 走看看