//首先定义一个借口
package com.test; public interface Fruit { }
//定义一个实现类 package com.test; public class Apple implements Fruit { } //定义实现类进行测试
package com.test; import java.util.ArrayList;
import java.util.List; public class TestName {
public static void main(String[] args) {
Fruit apple=new Apple();
System.out.println(apple.getClass().getCanonicalName());//返回com.test.Apple
System.out.println(apple.getClass().getSimpleName());//Apple
System.out.println(apple.getClass().getName());//返回com.test.Apple Apple[] arrApple=new Apple[]{};
System.out.println(arrApple.getClass().getCanonicalName());//返回com.test.Apple[]
System.out.println(arrApple.getClass().getSimpleName());//返回Apple[]
System.out.println(arrApple.getClass().getName());//返回[Lcom.test.Apple; System.out.println(String.class.getCanonicalName());//返回java.lang.String
System.out.println(String.class.getSimpleName());//返回String
System.out.println(String.class.getName());//返回java.lang.String System.out.println(int.class.getCanonicalName());//返回int
System.out.println(int.class.getSimpleName());//返回int
System.out.println(int.class.getName());//返回int Apple a1=new Apple();
Apple a2=new Apple();
List<Apple> appleList=new ArrayList<Apple>();
appleList.add(a1);
appleList.add(a2);
System.out.println(appleList.getClass().getCanonicalName());//返回java.util.ArrayList
System.out.println(appleList.getClass().getSimpleName());//返回ArrayList
System.out.println(appleList.getClass().getName());//返回java.util.ArrayList }
} //实际应用,运用泛型
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();
}
}
Class类,是获取类的类模板实例对象,通过反射的机制获取。
根据API中的定义,Class.getSimpleName()方法。是获取源代码中给出的‘底层类’简称
而Class.getName();以String的形式,返回Class对象的‘实体’名称
 
参考:http://sunyimaying0925-gmail-com.iteye.com/blog/768789

getClass 与getSimpleName的更多相关文章

  1. java 捕获所有异常

    1.) 通过捕获异常类型的基类Exception就可以处理所有类型的异常.(事实上还有其它的基类,但Exception是同编程活动相关的基类) 2.)因为Exception是与编程有关的所有异常类的基 ...

  2. Java class对象说明 Java 静态变量声明和赋值说明

        先看下JDK中的说明: java.lang.Object java.lang.Class<T> Instances of the class Class represent cla ...

  3. 用泛型方法Java从实体中提取属性值,以及在泛型方法中的使用

    public <T> T getFieldValue(Object target, String fieldName, Class<T> typeName) { try { O ...

  4. getCanonicalName和getSimpleName getName的区别与应用

    接口: package com.test; public interface Fruit { } 一个实现类: package com.test; public class Apple impleme ...

  5. getName()、getCanonicalName()、getSimpleName()异同

    package classes; class Box { class Inner { } } public class TestGetName { public static void main(St ...

  6. 随笔:关于Class.getSimpleName()

    最近学习过程中,遇到了Class.getSimpleName()这个方法,就搜索了一些资料: API定义: Class.getName():以String的形式,返回Class对象的"实体& ...

  7. java Class.getSimpleName() 的用法

    Usage in android: private static final String TAG = DemoApplication.class.getSimpleName(); public cl ...

  8. 关getClass().getClassLoader()

    InputStream   is   =   getClass().getClassLoader().getResourceAsStream("helloworld.properties&q ...

  9. super.getClass()方法调用

    下面程序的输出结果是多少?import java.util.Date;public class Test extends Date{public static void main(String[] a ...

随机推荐

  1. spark 操作hbase

    HBase经过七年发展,终于在今年2月底,发布了 1.0.0 版本.这个版本提供了一些让人激动的功能,并且,在不牺牲稳定性的前提下,引入了新的API.虽然 1.0.0 兼容旧版本的 API,不过还是应 ...

  2. Terminating app due to uncaught exception 'NSUnknownKeyException', reason: xxxx

    出现错误的情景: 使用Swift自定义Cell, 然后将这个Cell在OC中使用, 由于为了能够在OC中使用和使用起来命名比较规范 我在Swift自定义的Cell前加了 @objc(DSProduct ...

  3. C程序第二章节:算法

    1.主要讲了:算法,3种基本结构化的算法(顺序,选择,循环结构),N-S流程图表示算法,伪代码表示算法. 2.输入10个数,输出其中最大的一个数. #include <stdio.h>in ...

  4. UEFI模式下安装Win 7系统

    转载自:http://huayi898.blog.163.com/blog/static/2581351620144442319155/ 下载win7_64bit原版官方系统 1.用软碟通制作U盘启动 ...

  5. 解决Access restriction问题

    今天在导入一个包时报错了

  6. jQuery上传插件Uploadify 3.2使用

    Uploadify下载地址:http://www.uploadify.com/download/ 这里下载最新版的3.2的. 常用API描述: $(document).ready(function() ...

  7. image控件读取数据库二进制图片

    DataGridShowImage.aspx   <%@ Page language="c#" debug="true" Codebehind=" ...

  8. asp.net mvc+EF 递归生成树结构返回json

    0.数据表结构,主要属性有:Id.parentId(父节Id).Text.Url……等等. 1.新建一个树结构MenuModels public class MenuModels { private ...

  9. Linux_ERROR 1045 (28000): Access denied for user 'root'@'localhost'

    MySQL生成了root用户的随机密码(如下截图所示),并将这个随机密码放置在/root/.mysql_secret中.并且强制在第一次登陆时修改root用户的密码.Mysql 5.6及以后版本出处于 ...

  10. mobile 测试入门思维导图

    手机测试思维导图 ISO 测试思维导图 Android测试思维导图 原图出自:http://www.ministryoftesting.com/resources/mindmaps/