资源简介
代码主要实现了Java反射应用实例, 包含利用反射方法创建对象(使用默认构造函数和自定义构造函数),访问对应对象的方法(包括带参数的和不带参数的),访问对应对象的域(Field). 从这里可以看到代码的详细解说:http://blog.csdn.net/hejiangtao

代码片段和文件信息
/**
*Author: Jiangtao He; Email: ross.jiangtao.he@gmail.com
*File Name: MyReflect.java
*Date: 2012-1-9
*Copyright: All right reserved by author - Jiangtao He
*Version: MyJavaExpert v1.0
*/
package com.ross.reflect;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
/**
* Author: Jiangtao He; Email: ross.jiangtao.he@gmail.com
* Date: 2012-1-9
* Since: MyJavaExpert v1.0
* Description: reflect method implementation and test
*/
public class MyReflect
{
/**
* Author: Jiangtao He; Email: ross.jiangtao.he@gmail.com
* Date: 2012-1-9
* Description: Use reflect method to access the fields and methods of DataTypebean
*/
public static void main(String[] args) throws ClassNotFoundException
SecurityException NoSuchMethodException InstantiationException
IllegalAccessException IllegalArgumentException
InvocationTargetException
{
int iInt = 2012;
String sStr = “This a string!“;
String[] saStr = new String[] { “First item of the string array“
“Second item of the string array“
“Third item of the string array“ };
List oList = new ArrayList();
// Initialize the oList
int i = 0;
while (i < 3)
{
oList.add(i);
i++;
}
// get class
Class oClass = Class.forName(“com.ross.reflect.bean.DataTypeBean“);
// get the toString method a method without parameters
Method oToStringMethod = oClass.getMethod(“toString“);
// get the addDataToList method a method with parameters
Method oAddDataToListMethod = oClass.getMethod(“addDataToList“
int.class int.class);
// used default constructor to initialize a object
object oDefalutobject = oClass.newInstance();
// access fields process getDeclaredFields can access private and
// protected fields
Field[] oFields = oClass.getDeclaredFields();
for (int j = 0; j < oFields.length; j++)
{
// to access the private
oFields[j].setAccessible(true);
// getSimpleName method can get the type of the field according the
// field type set the data to the field
if (“int“.equals(oFields[j].getType().getSimpleName()))
{
oFields[j].setInt(oDefalutobject iInt);
}
else if (“String[]“.equals(oFields[j].getType().getSimpleName()))
{
oFields[j].set(oDefalutobject saStr);
}
else if (“String“.equals(oFields[j].getType().getSimpleName()))
{
oFields[j].set(oDefalutobject sStr);
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 310 2011-12-31 21:17 MyJavaExpert V1.0\.classpath
文件 393 2012-01-01 22:40 MyJavaExpert V1.0\.project
文件 22017 2011-12-30 00:58 MyJavaExpert V1.0\.settings\org.eclipse.jdt.core.prefs
文件 9884 2011-12-30 01:49 MyJavaExpert V1.0\.settings\org.eclipse.jdt.ui.prefs
文件 2868 2012-01-10 01:14 MyJavaExpert V1.0\bin\com\ross\reflect\bean\DataTypeBean.class
文件 3745 2012-01-10 01:14 MyJavaExpert V1.0\bin\com\ross\reflect\MyReflect.class
文件 2660 2012-01-10 01:14 MyJavaExpert V1.0\src\com\ross\reflect\bean\DataTypeBean.java
文件 4585 2012-01-10 01:14 MyJavaExpert V1.0\src\com\ross\reflect\MyReflect.java
目录 0 2012-01-10 00:15 MyJavaExpert V1.0\bin\com\ross\reflect\bean
目录 0 2012-01-10 00:15 MyJavaExpert V1.0\src\com\ross\reflect\bean
目录 0 2012-01-10 00:15 MyJavaExpert V1.0\bin\com\ross\reflect
目录 0 2012-01-10 00:15 MyJavaExpert V1.0\src\com\ross\reflect
目录 0 2012-01-04 23:27 MyJavaExpert V1.0\bin\com\ross
目录 0 2012-01-04 23:27 MyJavaExpert V1.0\src\com\ross
目录 0 2012-01-04 23:27 MyJavaExpert V1.0\bin\com
目录 0 2012-01-04 23:27 MyJavaExpert V1.0\src\com
目录 0 2011-12-29 23:37 MyJavaExpert V1.0\.settings
目录 0 2012-01-04 23:27 MyJavaExpert V1.0\bin
目录 0 2012-01-04 23:27 MyJavaExpert V1.0\src
目录 0 2012-01-03 23:54 MyJavaExpert V1.0
----------- --------- ---------- ----- ----
46462 20
相关资源
- java-图片识别 图片比较
- android毕业设计
- java23种设计模式+23个实例demo
- java Socket发送/接受报文
- JAVA828436
- java界面美化 提供多套皮肤直接使用
- 在线聊天系统(java代码)
- 基于Java的图书管理系统807185
- java中实现将页面数据导入Excel中
- java 企业销售管理系统
- java做的聊天系统(包括正规课程设计
- Java编写的qq聊天室
- 商店商品管理系统 JAVA写的 有界面
- JAVA开发聊天室程序
- 在linux系统下用java执行系统命令实例
- java期末考试试题两套(答案) 选择(
- JAVA3D编程示例(建模、交互)
- Java 文件加密传输
- java做的房产管理系统
- 基于jsp的bbs论坛 非常详细
- [免费]java实现有障碍物的贪吃蛇游戏
- java Servlet投票实例
- 操作系统作业 (pv,作业管理,等5个
- 基于C/S架构考试系统(Java)
- java access 仓库管理系统 源码
- 一元多项式相加 java实现
- java软件工程师简历模板——应届生
- DES(CFB) JAVA
- java读取局域网共享机器文件
- [免费]java学习 存储过程
评论
共有 条评论