【Java开源代码栏目提醒】:网学会员--在 Java开源代码编辑为广大网友搜集整理了:TextReflect.java绩等信息,祝愿广大网友取得需要的信息,参考学习。
package com.zf.s9;//创建一个包
import java.lang.reflect.*;//引入类
class Customer {// 用户类
private Long id;// 用户编号
private String name;// 用户名称
private int age;// 年龄
public Customer() {// 默认构造方法
}
public Customer(String name, int age) {// 带参数构造方法
this.name = name;
this.age = age;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void showInfo(String salary) {
System.out.println("薪水: " + salary + "RMB");
}
}
public class TextReflect {
public Object getProperty(Object obj) throws Exception {// 取得参数对象中的属性
// 获得对象的类型
Class<?> classType = obj.getClass();
System.out.println("Class:" + classType.getName());
// 通过默认构造方法创建一个新的对象
Object objectCopy = classType.getConstructor(new Class[] {})
.newInstance(new Object[] {});
// 获得对象的所有属性
Field fields[] = classType.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
String fieldName = field.getName();
String firstLetter = fieldName.substring(0, 1).toUpperCase();
// 获得和属性对应的getXXX()方法的名字
String getMethodName = "get" + firstLetter + fieldName.substring(1);
// 获得和属性对应的setXXX()方法的名字
String setMethodName = "set" + firstLetter + fieldName.substring(1);
// 获得和属性对应的getXXX()方法
Method getMethod = classType.getMethod(getMethodName,
new Class[] {});
// 获得和属性对应的setXXX()方法
Method setMethod = classType.getMethod(setMethodName,
new Class[] { field.getType() });
// 调用原对象的getXXX()方法
Object value = getMethod.invoke(obj, new Object[] {});
System.out.println(fieldName + ":" + value);
// 调用拷贝对象的setXXX()方法
setMethod.invoke(objectCopy, new Object[] { value });
}
return objectCopy;
}
public void getArrayProperty() throws ClassNotFoundException {
Class<?> classType = Class.forName("java.lang.String");
// 创建字符串数组
Object array = Array.newInstance(classType, 10);
// 把索引位置为5的元素设为"hello"
Array.set(array, 5, "helloworld");
// 获得索引位置为5的元素的值
String s = (String) Array.get(array, 5);
System.out.println(s);
}
public Object getPrivatePropertyValue(Object obj, String propertyName)
throws Exception {// 获取参数对象的属性值
Class cls = obj.getClass();// 获得对象的类型
Field field = cls.getDeclaredField(propertyName);// 获得对象的指定属性
field.setAccessible(true);// 属性允许访问
Object retvalue = field.get(obj);// 获得属性值
return retvalue;// 返回结果
}
public Object invokeMethod(Object owner, String methodName, Object[] args)
throws Exception {// 执行某对象的方法
Class cls = owner.getClass();// 获得对象的类型
Class[] argclass = new Class[args.length];// 对象参数
for (int i = 0, j = argclass.length; i < j; i++) {
argclass[i] = args[i].getClass();
}
Method method = cls.getMethod(methodName, argclass);// 获得对象方法
return method.invoke(owner, args);// 执行对象方法并返回
}
public int add(int num1, int num2) {// 计算和
return num1 + num2;
}
public String echo(String info) {//输出
return "echo: " + info;
}
public void invodeMethod() throws Exception {// 调用类的方法
Class<?> classType = TextReflect.class;
Object invokeTester = classType.newInstance();
Method addMethod = classType.getMethod("add", new Class[] { int.class,
int.class });// 调用TextReflect对象的add()方法
Object result = addMethod.invoke(invokeTester, new Object[] {
new Integer(100), new Integer(200) });//获得方法结果
System.out.println((Integer) result);
Method echoMethod = classType.getMethod("echo",
new Class[] { String.class });// 调用TextReflect对象的echo()方法
result = echoMethod.invoke(invokeTester, new Object[] { "Hello" });//获得方法结果
System.out.println((String) result);
}
public static void main(String[] args) throws Exception {//java程序主入口处
TextReflect tr = new TextReflect();//实例化对象
Customer customer = new Customer("lingda", 20);//对象初始化
customer.setId(new Long(1));
System.out.println("1.取得参数对象中的全部属性");
Customer cu = (Customer) tr.getProperty(customer);
System.out.println("用户信息:编号--" + cu.getId() + ",名称--" + cu.getName()
+ ",年龄--" + cu.getAge());
System.out.println("2.获得反射数组的信息");
tr.getArrayProperty();
System.out.println("3.获取参数对象的属性值");
String userName = (String) tr.getPrivatePropertyValue(customer, "name");
System.out.println("用户名称: " + userName);
System.out.println("4.执行某对象的方法");
tr.invokeMethod(customer, "showInfo", new Object[] { "2000" });
System.out.println("5.调用本类的方法");
tr.invodeMethod();
}
}
上一篇:
TextReadZiP.java
下一篇:
懂秋,在红尘深处.....