`
lgsun592
  • 浏览: 53729 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Java反射备忘录

    博客分类:
  • Java
阅读更多
在看spring的时候发现很多都是通过反射实现的,所以也看了看java关于反射的部分,总结如下,方便以后查找
Arithmetic.java
/**  
 * Class Arithmetic 
 * 反射目标类
 * 水平有限,所以此类纯是为了测试而写,无任何逻辑而言
 * @author  lgsun
 * Date: 2011-3-27
 */
package com.lgsun.target;

@SuppressWarnings("all")
public class Arithmetic
{
	private int	parmaerOne;
	private int	parmaerTwo;
	public int	sum;

	private Arithmetic()
	{
		this.parmaerOne = -1;
		this.parmaerTwo = -2;
	}

	public Arithmetic(int parmaerOne, int parmaerTwo)
	{
		this.parmaerOne = parmaerOne;
		this.parmaerTwo = parmaerTwo;
	}

	public int getParmaerOne()
	{
		return parmaerOne;
	}

	public void setParmaerOne(int parmaerOne)
	{
		this.parmaerOne = parmaerOne;
	}

	public int getParmaerTwo()
	{
		return parmaerTwo;
	}

	public void setParmaerTwo(int parmaerTwo)
	{
		this.parmaerTwo = parmaerTwo;
	}

	public int getSum()
	{
		sum = sum(parmaerOne, parmaerTwo);
		return this.sum;
	}

	public double getDivision()
	{
		return (parmaerOne / parmaerTwo);
	}

	private int sum(int p1, int p2)
	{
		return (p1 + p2);
	}

	public void getInfo()
	{
		System.out.println();
		System.out.println("parmaerOne=" + parmaerOne);
		System.out.println("parmaerTwo=" + parmaerTwo);
		System.out.println("sum=" + sum);
	}
}


ReflectConstructor.java
/**  
 * Class ReflectConstructor 
 * Constructor getConstructor(Class[] params) -- 获得使用特殊的参数类型的公共构造函数, 
 * Constructor[] getConstructors() -- 获得类的所有公共构造函数 
 * Constructor getDeclaredConstructor(Class[] params) -- 获得使用特定参数类型的构造函数(与接入级别无关) 
 * Constructor[] getDeclaredConstructors() -- 获得类的所有构造函数(与接入级别无关)
 * 前2个可以返回继承自父类的构造方法;后2个只能返回当前类定义的,并且包含私有方法
 * @author  lgsun
 * Date: 2011-3-27
 */
package com.lgsun.test;

import java.lang.reflect.Constructor;
import com.lgsun.target.Arithmetic;

public class ReflectConstructor
{
	@SuppressWarnings("all")
	public static void main(String[] args)
	{
		try
		{
			// 返回Arithmetic类全部的构造方法,包含私有构造方法
			Constructor[] con = Class.forName("com.lgsun.target.Arithmetic").getDeclaredConstructors();
			for (int i = 0; i < con.length; i++)
			{
				System.out.println(con[i].toGenericString());
			}

			// 通过Arithmetic(int p1,int p2)构造方法创建实例
			Class[] type = new Class[] { int.class, int.class };
			Constructor pCon1 = Class.forName("com.lgsun.target.Arithmetic").getDeclaredConstructor(type);
			Arithmetic arit1 = (Arithmetic) pCon1.newInstance(3, 4);
			arit1.getInfo();

			// 通过Arithmetic()构造方法创建实例
			Constructor pCon2 = Class.forName("com.lgsun.target.Arithmetic").getDeclaredConstructor();
			// 由于此构造方法是私有的,所以必须设置Accessible为true
			pCon2.setAccessible(true);
			Arithmetic arit2 = (Arithmetic) pCon2.newInstance();
			arit2.getInfo();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}


ReflectMethod.java
/**  
 * Class ReflectMethod 
 * Method getMethod(String name, Class[] params) -- 使用特定的参数类型,获得命名的公共方法 
 * Method[] getMethods() -- 获得类的所有公共方法 
 * Method getDeclaredMethod(String name, Class[] params) -- 使用特写的参数类型,获得类声明的命名的方法 
 * Method[] getDeclaredMethods() -- 获得类声明的所有方法
 * 前2个方法会返回所有的属性,包括父类属性;后2个只会返回此类中定义的属性,包含私有属性
 * @author  lgsun
 * Date: 2011-3-28
 */
package com.lgsun.test;

import java.lang.reflect.Method;

import com.lgsun.target.Arithmetic;

@SuppressWarnings("all")
public class ReflectMethod
{
	public static void main(String[] args)
	{
		Arithmetic arithmetic = new Arithmetic(7, 8);
		Method[] methods = arithmetic.getClass().getDeclaredMethods();
		for (int i = 0; i < methods.length; i++)
		{
			System.out.println(methods[i].toGenericString());
		}

		try
		{
			Class[] types = new Class[] { int.class, int.class };
			Method method1 = arithmetic.getClass().getDeclaredMethod("sum", types);
			method1.setAccessible(true);
			// 这样写是错误的
			// int[] parms=new int[]{17,18};
			Object[] parms = new Object[] { 17, 18 };
			Object result = method1.invoke(arithmetic, parms);

			System.out.println(((Integer) result).intValue());
			arithmetic.getInfo();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

	}
}


ReflectFiled.java
/**  
 * Class ReflectFiled 
 * Field getField(String name) -- 获得命名的公共字段 
 * Field[] getFields() -- 获得类的所有公共字段 
 * Field getDeclaredField(String name) -- 获得类声明的命名的字段 
 * Field[] getDeclaredFields() -- 获得类声明的所有字段
 * 同样,前2个方法会返回所有的属性,包括父类属性;后2个只会返回此类中定义的属性,包含私有属性
 * @author  lgsun
 * Date: 2011-3-28
 */
package com.lgsun.test;

import java.lang.reflect.Field;

import com.lgsun.target.Arithmetic;

public class ReflectFiled
{
	@SuppressWarnings("all")
	public static void main(String[] args)
	{
		// 返回Arithmetic类全部属性,包含私有属性
		Arithmetic arithmetic = new Arithmetic(4, 5);
		Field[] fields = arithmetic.getClass().getDeclaredFields();
		for (int i = 0; i < fields.length; i++)
		{
			System.out.println(fields[i].toGenericString());
		}

		try
		{
			// 直接给public属性sum赋值
			Field field1 = arithmetic.getClass().getDeclaredField("sum");
			field1.set(arithmetic, 19);
			arithmetic.getInfo();

			// 给private属性parmaerOne赋值
			Field field2 = arithmetic.getClass().getDeclaredField("parmaerOne");
			// 必加
			field2.setAccessible(true);
			field2.set(arithmetic, 14);
			arithmetic.getInfo();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}


附件为工程文件
参考http://www.ibm.com/developerworks/cn/java/j-dyn0603/
0
0
分享到:
评论

相关推荐

    java开发备忘录

    java开发备忘录

    日历备忘录Java源码

    JAVA程序日历源码,层次清晰,编写规范

    swing javadb 备忘录 记事本 临时记事

    一个自己写的备忘录程序,可以用标题和内容的形式记录一些信息,可以按照标题、记录日期、信息类型查询,导出excel数据。另外提供到期提示和闹钟功能。使用javadb嵌入式数据库,swing外观。提供Eclipse工程源代码和...

    备忘录模式代码示例

    备忘录模式的示例代码和文档,学习备忘录模式的参考资料。

    Java日历及备忘录

    Java写的一个类似windows自带的日历程序,他除了可以浏览日期外,还可以在特定的时间点写备忘录,当到达该时间点时会有弹窗提示。

    用JAVA编写的备忘录

    用JAVA编写的备忘录, 可以记录下你每天做的事情,可以查看你以前做的事情,很有帮助

    java实现备忘时钟

    用java实现备忘时钟,适合初学者,用java实现备忘时钟,适合初学者,

    微信小程序 备忘录 (源码)

    微信小程序 备忘录 (源码)微信小程序 备忘录 (源码)微信小程序 备忘录 (源码)微信小程序 备忘录 (源码)微信小程序 备忘录 (源码)微信小程序 备忘录 (源码)微信小程序 备忘录 (源码)微信小程序 备忘录 (源码)微信小...

    小程序源码 备忘录 (代码+截图)

    小程序源码 备忘录 (代码+截图)小程序源码 备忘录 (代码+截图)小程序源码 备忘录 (代码+截图)小程序源码 备忘录 (代码+截图)小程序源码 备忘录 (代码+截图)小程序源码 备忘录 (代码+截图)小程序源码 备忘录 (代码+...

    微信小程序源码 备忘录(学习版)

    微信小程序源码 备忘录(学习版)微信小程序源码 备忘录(学习版)微信小程序源码 备忘录(学习版)微信小程序源码 备忘录(学习版)微信小程序源码 备忘录(学习版)微信小程序源码 备忘录(学习版)微信小程序源码 备忘录(学习...

    微信小程序 小工具类 备忘录 (源代码+截图)

    微信小程序 小工具类 备忘录 (源代码+截图)微信小程序 小工具类 备忘录 (源代码+截图)微信小程序 小工具类 备忘录 (源代码+截图)微信小程序 小工具类 备忘录 (源代码+截图)微信小程序 小工具类 备忘录 (源...

    备忘录日程管理java代码

    基于java的备忘录软件开发,还有日程管理的功能

    毕业设计基于JavaWeb实现的一个备忘录系统项目源码.zip

    毕业设计基于JavaWeb实现的一个备忘录系统项目源码。难度适中,新手自己可操作 备忘录 介绍 后端部分基于Servlet、Jdbc实现. 前端部分基于Layui、jqury实现。 一个简单的前后端分离Demo,前后端交互JSON数据格式 ...

    java备忘录

    当初学习JAVA时候,敲得一个黑窗口JAVA备忘录,用的HASHMAP对数据进行增删改查。

    备忘录JAVA代码

    实现备忘录基本功能,可以显示年月日日历,点击任意一日期可以写备忘录,写完保存后在点这个日期则跳出窗口说该日期有备忘录

    Android开发备忘录

    设计和实现一个类似个人备忘录的 Android APP ,数据库采用 SQLite (也可以直接访问 Web 端 MySQL 数据库、或提供 Web 接口访问 MySQL 数据库)。 1.用户注册和登录(这类 APP 一般面对个人,用户不需要分类别); ...

    Java设计模式-备忘录

    Java设计模式,备忘录模式的Demo,具体的思想与实现有很多讲得很好的老师,我是看厉风行老师的视频学的,讲得不错,简单易懂

    微信小程序-------备忘录

    微信小程序备忘录,备忘录备忘录备忘录备忘录备忘录备忘录备忘录备忘录备忘录备忘录备忘录备忘录备忘录备忘录备忘录备忘录备忘录备忘录备忘录备忘录备忘录备忘录备忘录备忘录备忘录备忘录备忘录备忘录备忘录备忘录...

    Java 综合案例 可编辑备忘录的日历程序 学生系统

    这是几个重要的、非常有用的Java综合案例,里面有带有以文本形式的可编辑备忘录的日历程序,有详细的学生系统设计,有网络编程方面的实例,程序中各个模块划分详细,希望对大家有用!

Global site tag (gtag.js) - Google Analytics