簡單比較 getName()、getCanonicalName()、getSimpleName() 的異同


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.TestClass
		System.out.println(testClass.getClass().getCanonicalName());// com.ershuai.stu.other.TestClass
		System.out.println(testClass.getClass().getSimpleName());// TestClass
		
		System.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.ArrayList
		System.out.println(appleList.getClass().getCanonicalName());// java.util.ArrayList
		System.out.println(appleList.getClass().getSimpleName());// ArrayList
		
		System.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$NTestClass
		System.out.println(ntestClass.getClass().getCanonicalName());// com.ershuai.stu.other.Test0.NTestClass
		System.out.println(ntestClass.getClass().getSimpleName());// NTestClass
		
		System.out.println("\n************************************************************************");

		System.out.println(Integer.class.getName());// java.lang.Integer
		System.out.println(Integer.class.getCanonicalName());// java.lang.Integer
		System.out.println(Integer.class.getSimpleName());// Integer
		
		System.out.println("\n************************************************************************");

		System.out.println(int.class.getName());// int
		System.out.println(int.class.getCanonicalName());// int
		System.out.println(int.class.getSimpleName());// int
	}
	
	public static class NTestClass {
		
	}
}

輸出

******************************** 普通類 ****************************************
com.ershuai.stu.other.TestClass
com.ershuai.stu.other.TestClass
TestClass

******************************** 普通類  List ****************************************
java.util.ArrayList
java.util.ArrayList
ArrayList

******************************** 普通類  array - 有區別 ****************************************
[Lcom.ershuai.stu.other.TestClass;
com.ershuai.stu.other.TestClass[]
TestClass[]

******************************** 內部類 - 有區別  ****************************************
com.ershuai.stu.other.Test0$NTestClass
com.ershuai.stu.other.Test0.NTestClass
NTestClass

************************************************************************
java.lang.Integer
java.lang.Integer
Integer

************************************************************************
int
int
int


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();  
    }  
}  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章