Java SE之反射技术[Class,Field](一)
一、什么是反射?
反射库(Reflection Library)提供了一个非常丰富且精心设计的工具集,以便编写能够动态操纵Java代码的程序。这项功能被大量地应用在JavaBeans中,它是Java组件的体系结构(有关Java Beans的详细内容见Java II卷)。使用反射,Java可以支持Visual Basic 用户习惯使用的工具。特别是再设计或运行中添加新类时,能够快速地应用开发工具动态地查询新添加类的能力。
能够分析类能力的程序成为反射(Reflective)。反射机制的功能极其强大,在下面可以看到,反射机制可以用来:
①在运行中分析类的能力
②在运行中查看类对象,例如,编写一个toString()方法提供所有类使用
③实现通用数组操作代码
④利用Method对象,这个对象很像C++中的函数指针。
反射是一种功能强大且复杂的机制。使用它的主要人员是工具构造者,而不是应用程序员。
-----《Java核心技术卷 I 基础知识》
(手打)
二、引用的类与库
库:java.lang.reflect.*
类:
① Class [Java.lang.Class]
② Field [java.lang.reflect.Field]
③ Method [java.lang.reflect.Method]
④ Constructor [java.lang.reflect.Constructor ]
⑤ Modifier [java.lang.reflect.Modifier]
⑥ AccessibleObject [java.lang.reflect.AccessibleObject]
三、常用方法总结
四、反射实例[测试]
/**
* @author:Johnny Zen
* @date:2017-06-09
* @description:一个实体类(Person)
**/
import java.lang.reflect.Field; import javax.activation.FileDataSource; import com.sun.org.apache.bcel.internal.generic.NEW; public class Person extends Entity{
private String name;
private String password;
private String sex;
public String telephone; public Person() {
this.name = "测试名字:哇哈哈";
this.password = "测试密码:783234";
this.sex = "测试性别:男";
this.telephone = "测试电话:2947298569365";
} public Person(String name, String password, String sex, String telephone) {
super();
this.name = name;
this.password = password;
this.sex = sex;
this.telephone = telephone;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} public String getTelephone() {
return telephone;
} public void setTelephone(String telephone) {
this.telephone = telephone;
} @Override
public String toString() {
return "Person [name=" + name + ", password=" + password + ", sex=" + sex + ", telephone="
+ telephone + "]";
} } abstract class Entity{ }
然后测试:
public static void main(String args[]) throws IllegalAccessException{ Person person1 = new Person("王华","123456","男","15902848904");
Person person2 = new Person();
Person person3 = new Person();
try {
System.out.println("对getClass()所有方法的遍历:");
System.out.println("person1.getClass().desiredAssertionStatus():"+person1.getClass().desiredAssertionStatus()); //期望的断言状态 System.out.println("person1.getClass().equals(person2):"+person1.getClass().equals(person2));
System.out.println("person1.getClass().equals(person1):"+person1.getClass().equals(person1));
System.out.println("person2.getClass().equals(person3):"+person2.getClass().equals(person3)); System.out.println("person1.getClass().getCanonicalName():"+person2.getClass().getCanonicalName());//获取权威正规的类名 System.out.println("person1.getClass().getModifiers():"+person1.getClass().getModifiers());
System.out.println("person1.getClass().getName():"+person1.getClass().getName());
System.out.println("person1.getClass().getSimpleName():"+person1.getClass().getSimpleName());
System.out.println("person1.getClass().getTypeName():"+person1.getClass().getTypeName()); System.out.println("person1.getClass().hashCode():"+person1.getClass().hashCode());//获取该类的hashcode值
System.out.println("person2.getClass().hashCode():"+person2.getClass().hashCode());
System.out.println("person3.getClass().hashCode():"+person3.getClass().hashCode()); System.out.println("person1.getClass().toGenericString():"+person1.getClass().toGenericString());
System.out.println("person1.getClass().toString():"+person1.getClass().toString()); System.out.println("person1.getClass().isAnnotation():"+person1.getClass().isAnnotation()); //是否为注解类型
// System.out.println("【person1.getClass().asSubclass(null)】:"+person1.getClass().asSubclass(null)); //疑问?
// System.out.println("【person1.getClass().cast(person1)】:"+person1.getClass().cast(person1)); //疑问?
// System.out.println("person1.getClass().cast(person1):"+person1.getClass().cast(person1)); //疑问?
// System.out.println("person1.getClass().isAnnotationPresent(null):"+person1.getClass().isAnnotationPresent(null)); //疑问? System.out.println("person1.getClass().isAnonymousClass():"+person1.getClass().isAnonymousClass()); //是否是匿名类
System.out.println("(new int[2]).getClass().isAnonymousClass():"+(new int[2]).getClass().isAnonymousClass()); //是否是匿名类 System.out.println("person1.getClass().isArray():"+person1.getClass().isArray()); //是否是数组类
System.out.println("(new int[2]).getClass().isArray():"+(new int[2]).getClass().isArray()); //是否是数组类 /*重点方法:getFields():*/
System.out.println("***************************************************************************************************");
System.out.println("person1.getClass().getFields()[0].getName():"+person1.getClass().getFields()[0].getName()); //获取公共属性的第(0+1)个属性 Field fields1[] = person1.getClass().getFields(); //仅仅能获取到public属性的field
// System.out.println("fields1[1].get(person2):"+fields1[1].get(person2)); //由于第二个域获取不到,域数组长度为1,故产生数组超界
Field fields2[] = person1.getClass().getDeclaredFields(); //获取到所有声明的field(包括private的)
System.out.println("fields2[1].get(person2):"+fields2[1].get(person2));
System.out.println("fields2[1].get(person1):"+fields2[1].get(person1)); System.out.println("fields2[1].getName():"+fields2[1].getName());
System.out.println("fields2[1].hashCode():"+fields2[1].hashCode());
System.out.println("fields2[1].toString():"+fields2[1].toString()); System.out.println("fields2[1].equals(person2):"+fields2[1].equals(person2));
System.out.println("fields2[1].equals(fields2[1]):"+fields2[1].equals(fields2[1]));//field是否相同 System.out.println("fields2[1].getType():"+fields2[1].getType()); 58 System.out.println("fields[1].getType().getSimpleName()"+fields[1].getType().getSimpleName());
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
} }
结果:
对getClass()所有方法的遍历:
person1.getClass().desiredAssertionStatus():false
person1.getClass().equals(person2):false
person1.getClass().equals(person1):false
person2.getClass().equals(person3):false
person1.getClass().getCanonicalName():Person
person1.getClass().getModifiers():1
person1.getClass().getName():Person
person1.getClass().getSimpleName():Person
person1.getClass().getTypeName():Person
person1.getClass().hashCode():914424520
person2.getClass().hashCode():914424520
person3.getClass().hashCode():914424520
person1.getClass().toGenericString():public class Person
person1.getClass().toString():class Person
person1.getClass().isAnnotation():false
person1.getClass().isAnonymousClass():false
(new int[2]).getClass().isAnonymousClass():false
person1.getClass().isArray():false
(new int[2]).getClass().isArray():true
***************************************************************************************************
person1.getClass().getFields()[0].getName():telephone
fields2[1].get(person2):测试密码:783234
fields2[1].get(person1):123456
fields2[1].getName():password
fields2[1].hashCode():-960414226
fields2[1].toString():private java.lang.String Person.password
fields2[1].equals(person2):false
fields2[1].equals(fields2[1]):true
fields2[1].getType():class java.lang.String
31 fields[1].getType().getSimpleName():String
五、参考文档
Java核心技术之反射(详细API和MyBatis简单实现)
Java SE之反射技术[Class,Field](一)的更多相关文章
- Java SE之反射技术[Field](二)
如果对于反射的基本概念还不了解的请见上一帖子.本文仅谈fields的用法demo /** * * @author Zen Johnny * */ package com.cpms.test; impo ...
- Java SE之反射技术[Class](三)
/** * * @author Zen Johnny * */ package com.cpms.test; import java.lang.reflect.Field; import java.u ...
- 第89节:Java中的反射技术
第89节:Java中的反射技术 反射技术是动态的获取指定的类,和动态的调用类中的内容(没有类前就可以创建对象,将对象的动作完成,这就是动态的获取指定的类). 配置文件把具体实现的类名称定义到配置文件中 ...
- 黑马程序员:Java编程_反射技术
=========== ASP.Net+Android+IOS开发..Net培训.期待与您交流!=========== Java类用于描述一类事物的共性,该类事物有什么属性,没有什么属性,至于这个属性 ...
- JAVA:将反射技术应用于工厂模式(Factory)和单例模式(Singleton)的简单代码
反射技术大量用于Java设计模式和框架技术,最常见的设计模式就是工厂模式(Factory)和单例模式(Singleton). 参考URL: http://blog.csdn.net/xiaohai79 ...
- 【转】【Java】利用反射技术,实现对类的私有方法、变量访问
java关于反射机制的包主要在java.lang.reflect中,structs,hibernate,spring等框架都是基于java的反射机制. 下面是一个关于利用java的反射机制,实现了对私 ...
- java 基础之 反射技术
1. java代码 在 java 语言中最核心的就是代码的运行, 按照面向对象的思想,在调用java代码时往往需要先创建对象,再调用方法, 而写在方法中的即所谓的java 代码 一段java代码在程序 ...
- Java JDBC利用反射技术将查询结果封装为对象
1.JDBC将返回结果集封装成对象demo class JdbcDemo { /** * 获取数据库列名 * @param rs * @return */ private static String[ ...
- Java高新技术第二篇:反射技术
今天我们来看一下Java中的反射技术: 首先来了解一下Java中的反射的一些概念: Java中的反射是1.2引入的 反射的基石:class类 Class类的各个实例对象分别对应各个类在内存中的字节码, ...
随机推荐
- jquery属性操作,应用,事件,扩展extend,动画效果(二)
一.相关知识点总结1.CSS .css() - .css("color") -> 获取color css值 - .css("color", & ...
- JavaScript(JS)之Javascript对象DOM(五)
https://www.cnblogs.com/haiyan123/p/7653032.html 一.JS中for循环遍历测试 for循环遍历有两种 第一种:是有条件的那种,例如 for(var ...
- django 通过ajax完成邮箱用户注册、激活账号
一.图片验证码 django-simple-captcha配置 1.在pycharm中,File====>Settings====>Project:项目名====>Project I ...
- Fiddler安装证书
1.打开Fiddler,点击工具栏中的Tools——>Fiddler Options… 2.切换到 HTTPS 选项卡,勾选 Capture HTTPS CONNECTs,勾选 De ...
- 逻辑回归 代价函数J关于系数theta求导
J=-y*loga-(1-y)*log(1-a) 梯度下降,求dJ/d_theta_j
- MySQL STR_TO_DATE函数
转: MySQL STR_TO_DATE函数 2017年12月05日 15:41:58 木林森淼 阅读数:23822 版权声明:水平有限,如有补充或更正,望大家评论指正 https://blog. ...
- JS重点整理之JS原型链彻底搞清楚
对象 要清楚原型链,首先要弄清楚对象: 普通对象 最普通的对象:有__proto__属性(指向其原型链),没有prototype属性. 原型对象(person.prototype 原型对象还有cons ...
- 动态样式语言—LESS基础知识
CSS是一门非程序式语言,缺少逻辑性,不便于维护 LESS在CSS现有语法的基础上,为CSS加入程序式语言的特性 引入了变量.混入.运算.函数等功能,大大简化CSS的编写,降低了CSS的维护成本 LE ...
- Redis作为lru缓存作用
当 Redis 作为缓存使用时,当你添加新的数据时,有时候很方便使 Redis 自动回收老的数据.LRU 实际上是被唯一支持的数据移除方法.Redis 的 maxmemory 指令,用于限制内存使用到 ...
- SpringMvc的基本流程
1.流程图 2.特别说明 1)SpringMvc有6大组件(MVC各一个,再加3个运用策略模式的组件) 2)MVC对应的组件分别是(Handler.View.DisapatchServelet) 3) ...