简单比较 getName()、getCanonicalName()、getSimpleName() 的异同

来源:互联网 发布:上海巨人网络垃圾公司 编辑:程序博客网 时间:2024/06/12 18:26


public class Test0 {public static void main(String[] args) {System.out.println("******************************** 普通类 ****************************************");TestClass testClass = new TestClass();System.out.println(testClass.getClass().getName());// com.ershuai.stu.other.TestClassSystem.out.println(testClass.getClass().getCanonicalName());// com.ershuai.stu.other.TestClassSystem.out.println(testClass.getClass().getSimpleName());// TestClassSystem.out.println("\n******************************** 普通类  List ****************************************");TestClass a1 = new TestClass();TestClass a2 = new TestClass();List<TestClass> appleList = new ArrayList<TestClass>();appleList.add(a1);appleList.add(a2);System.out.println(appleList.getClass().getName());// java.util.ArrayListSystem.out.println(appleList.getClass().getCanonicalName());// java.util.ArrayListSystem.out.println(appleList.getClass().getSimpleName());// ArrayListSystem.out.println("\n******************************** 普通类  array - 有区别 ****************************************");TestClass[] arrTestClass = new TestClass[] {};System.out.println(arrTestClass.getClass().getName());// [Lcom.ershuai.stu.other.TestClass;System.out.println(arrTestClass.getClass().getCanonicalName());// com.ershuai.stu.other.TestClass[]System.out.println(arrTestClass.getClass().getSimpleName());// TestClass[]System.out.println("\n******************************** 内部类 - 有区别  ****************************************");NTestClass ntestClass = new NTestClass();System.out.println(ntestClass.getClass().getName());// com.ershuai.stu.other.Test0$NTestClassSystem.out.println(ntestClass.getClass().getCanonicalName());// com.ershuai.stu.other.Test0.NTestClassSystem.out.println(ntestClass.getClass().getSimpleName());// NTestClassSystem.out.println("\n************************************************************************");System.out.println(Integer.class.getName());// java.lang.IntegerSystem.out.println(Integer.class.getCanonicalName());// java.lang.IntegerSystem.out.println(Integer.class.getSimpleName());// IntegerSystem.out.println("\n************************************************************************");System.out.println(int.class.getName());// intSystem.out.println(int.class.getCanonicalName());// intSystem.out.println(int.class.getSimpleName());// int}public static class NTestClass {}}

输出

******************************** 普通类 ****************************************com.ershuai.stu.other.TestClasscom.ershuai.stu.other.TestClassTestClass******************************** 普通类  List ****************************************java.util.ArrayListjava.util.ArrayListArrayList******************************** 普通类  array - 有区别 ****************************************[Lcom.ershuai.stu.other.TestClass;com.ershuai.stu.other.TestClass[]TestClass[]******************************** 内部类 - 有区别  ****************************************com.ershuai.stu.other.Test0$NTestClasscom.ershuai.stu.other.Test0.NTestClassNTestClass************************************************************************java.lang.Integerjava.lang.IntegerInteger************************************************************************intintint


1、getCanonicalName顾名思义的正规的名字,与之对应的是getName

2、大部分情况下,getName和getCanonicalName没有什么不同的, 但是对于array和内部类就不一样了

3、对于数组:getCanonicalName是正规的(最后带有[]表示数组),getName是编译器的(前面带有[表示一维数组)

4、对于内部类:getCanonicalName是空,getName是带有$的

5、getSimpleName是简单的名字,是getName去掉了包名和$(内部类时候带有$)的余下的类自身的名字;getName带有包名和$(内部类时候带有$)



实际应用: hql的泛型查询

public <T> List<T> getRecords(Class<T> c,Date startDate,Date endDate){          StringBuilder hql = new StringBuilder("select t from ");          hql.append(c.getCanonicalName());          hql.append(" t where t.statTime>=:startTime and t.statTime<:endTime ");            Query query = sessionFactory.getCurrentSession().createQuery(hql.toString());          query.setParameter("startTime", startDate);          query.setParameter("endTime", endDate);                    return query.list();      }  }  


阅读全文
0 0
原创粉丝点击