package com.itheima;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class InstroSpectorText {
public static void main(MainParameter parameterObject) throws Exception{
ReflectPoint rpt1 =new ReflectPoint(3,5);
String PropertyName = "x";
getProperty(rpt1, PropertyName);
int x = 7;
setProperty(rpt1, PropertyName, x);
}
private static void setProperty(ReflectPoint rpt1, String PropertyName,
int x) throws IntrospectionException, IllegalAccessException,
InvocationTargetException {
PropertyDescriptor pd1 = new PropertyDescriptor(PropertyName, ReflectPoint.class);
Method pw = pd1.getWriteMethod();
pw.invoke(rpt1, x);
System.out.println(rpt1.getX());
}
private static void getProperty(ReflectPoint rpt1, String PropertyName)
throws IntrospectionException, IllegalAccessException,
InvocationTargetException {
PropertyDescriptor pd = new PropertyDescriptor(PropertyName, ReflectPoint.class);
Method pr = pd.getReadMethod();
System.out.println(pr.invoke(rpt1));
}
}
我是按章张老师的视频一步步写的,重构了方法之后就运行不了,提示出现重大错误,系统即将停止!怪吓人的
|
|