本文主要是介绍简单比较 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.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.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(); }
}
这篇关于简单比较 getName()、getCanonicalName()、getSimpleName() 的异同的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!