getCanonicalName和getSimpleName getName的区别与应用
接口:
- 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
}
}
实际应用: 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();
- }
- }
getCanonicalName和getSimpleName getName的区别与应用的更多相关文章
- Thread.currentThread().getName() ,对象实例.getName() 和 this.getName()区别
原文链接:http://www.cnblogs.com/signheart/p/922dcf75dd5fe6b418d4475af89c4664.html 使用Thread.currentThread ...
- getName()、getCanonicalName()、getSimpleName()异同
package classes; class Box { class Inner { } } public class TestGetName { public static void main(St ...
- Java中class的getName()和getCanonicalName()两个方法的区别
getName()返回的是虚拟机里面的class的表示 getCanonicalName()返回的是更容易理解的表示 对于普通类来说,二者没什么区别,只是对于特殊的类型上有点表示差异 比如byte[] ...
- 在Java中,关于.Class()与.getCalss与.getClass().getName()的区别
** * Java反射 *所谓反射,可以理解为在运行时期获取对象类型信息的操作. *传统的编程方法要求程序员在编译阶段决定使用的类型,但是在反射的帮助下, *编程人员可以动态获取这些信息,从而编写更加 ...
- Java多线程中thread.getname()和thread.currentThread().getName();的去别
首先要明白 this.XXX 的使用场景 使用Thread.currentThread().getName()和使用this.getName()和对象实例.getName(),都可以得到线程的名称,但 ...
- 深入分析Java反射(一)-核心类库和方法
前提 Java反射的API在JavaSE1.7的时候已经基本完善,但是本文编写的时候使用的是Oracle JDK11,因为JDK11对于sun包下的源码也上传了,可以直接通过IDE查看对应的源码和进行 ...
- 反射——类(Class)
本文原创,转载请注明原处! 红色代表的是基本组件:包(Package),修饰符(modifier),类(Class),字段(Field),构造器(Constructor)和方法(Method). 黄色 ...
- TSharding源码阅读
需要的背景知识:Spring 和Mybatis 实现原理和源码, javaassist字节码增强的使用, java及设计模式的使用 1 读取解析数据库配置文件 DataSourceScanner实现了 ...
- Hibernate 再接触 核心开发接口
1.可以重载方法进行配置文件的指定 sessionFactory = new AnnotationConfiguration().configure("hibernate.xml" ...
随机推荐
- 实战搭建SVN代码版本服务器
前言:公司要求搭建一台SVN代码版本管理服务器,用于管理所有代码资产: 项目架构图 1.环境安装 [root@host_centos ~]#yum –y install subversion mod_ ...
- 集群(cluster)和高可用性(HA)的概念
1.1 什么是集群 简单的说,集群(cluster)就是一组计算机,它们作为一个整体向用户提供一组网络资源.这些单个的计算机系统就是集群的节点(node).一个理想的集群是,用户从来不会意识到集群系统 ...
- 解决ntp的错误:no server can be used,exiting
台湾地区一台机器进行时间同步时,出现下面的错误.很显然,它提示的是不能找到主机time.stdtime.gov.tw. 所以我初步猜测是由于dns解析的问题 1 2 3 4 # /usr/sbi ...
- ubuntu 下安装 texlive 并设置 ctex 中文套装
1 安装 texlive2013 1.1 下载 texlive2013 下载地址:http://ftp.ctex.org/mirrors/CTAN/systems/texlive/Images/tex ...
- 【CSS】使用盒模型
盒子是CSS中的基础概念,我们需要使用它来配置元素的外观以及文档的整体布局. 1. 为元素应用内边距 应用内边距会在元素内容和边距之间添加空白.我们可以为内容盒的每个边界单独设置内边距,或者使用 pa ...
- 在github上建立自己的网站
学了前端小半年,如今写了个自己的网页想要去应聘,却发现部署很麻烦,部署到阿里云之类,买域名啊啥的还要收费,说贵也不贵,但我就是傲娇~ google一下了解到Github有一个Github pages的 ...
- HTTPS----安全超文本传输协议
HTTPS协议详解HTTPS以保密为目标研发,简单讲是HTTP的安全版.其安全基础是SSL协议,因此加密的详细内容请看SSL.全称Hypertext Transfer Protocol over Se ...
- u3d_Shader_effects笔记6 第四章 使用cubeMap简单的反射读取
一:前面心情: 1.今天开了个小会,该看的继续要看,不要堕落. 2.还有就是丽的生活习惯不太好.慢慢改变. 3.哎,公司人员争夺吗?哎,不知道,不了解,不去想,提升自己,内心明净 二.主要内容和参考 ...
- selenium自动化-java-IE启动
这是一个方法,直接在main调用就可以 private static void ie() { WebDriver Driver; // ie启动成功,files是启动ie驱动 ...
- jdbc java数据库连接 5)CallableStatement 接口
CallableStatement执行存储过程(也是预编译语言) 首先在sql中执行以下带有输入参数的代码: DELIMITER $ CREATE PROCEDURE pro_findById(IN ...