JavaBean_重构_内省操作

类加载器的方式管理资源和配置文件
    InputStream ips = (InputStream)
  

ReflectTest.class.getClassLoader().getResourceAsStream("包名/文件名");//字节码.类加载器.取相对路径.文件名
   
 InputStream ipc =  (InputStream)
    

ReflectTest.class.getResourceAsStream("resources/文件名" ); //字节码.取相对路径("子路经/文件名")
  
  
 InputStream iph =  (InputStream)
    

ReflectTest.class.getResourceAsStream("/包名/子路经/文件名");//字节码.取相对路径("/包名/子路经/文件名名");  前面有"/"的,表示根路径,需要写上包名/文件名。

 

JavaBean的属性名命名规则:
Age-->如果除去“get”“set”后剩下的字母段,第二个

字母是小写的,那么该JavaBean属性名就应该是小写的。
例如:
gettime-->time
getTime-->time
getCPU-->CPU


package cn.itcast.day1;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import

java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class IneroSpectorTest {
 /**
  * @param args
  * @throws IntrospectionException
  * @throws InvocationTargetException
  * @throws IllegalAccessException
  * @throws IllegalArgumentException
  */
 public static void main(String[] args)

throws
 IntrospectionException,

IllegalArgumentException,

IllegalAccessException,

InvocationTargetException {
  // TODO Auto-generated method

stub
  ReflectPoint pt1 = new

ReflectPoint(2, 7);    //调用构造方法创建实验对象String propertyName = "y";       //这里把需要操作的属性名单独赋值,可能是为了以后更改方便吧     
  PropertyDescriptor pd = new
      PropertyDescriptor(propertyName, pt1.getClass());  //参数为“属性名”“类名”
  Method methodGetY = pd.getReadMethod();       //把PropertyDescriptor类的其中一个属性getReadMethod  赋值给一个变量
  Object retVal = methodGetY.invoke(pt1);     //把以上操作包装成一个对象,并使用该对象的invoke()方法取值。
  System.out.println(retVal);
 }
}


重构_内省的简单应用

package cn.itcast.day1;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import

java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class IneroSpectorTest {
 /**
  * @param args
  * @throws IntrospectionException
  * @throws InvocationTargetException
  * @throws IllegalAccessException
  * @throws IllegalArgumentException
  */
 public static void main(String[] args)

throws
 IntrospectionException,

IllegalArgumentException,

IllegalAccessException,

InvocationTargetException {
  // TODO Auto-generated method

stub
  ReflectPoint pt1 = new ReflectPoint(4, 7);    //调用构造方法创建实验对象
  String propertyName = "y";       //这里把需要操作的属性名单独赋值,可能是为了以后更改方便吧     
  PropertyDescriptor pd = getPropert(pt1, propertyName);     //把以上操作包装成一个对象,并使用该对象的invoke()方法取值。
  //System.out.println(retVal);
  
  Object value = 7;
  
  setProperties(pt1, propertyName, value);
  
  System.out.println(pt1.getX());
 }

 private static void setProperties(Object pt1, String propertyName,Object value) throws IntrospectionException,
   IllegalAccessException,

InvocationTargetException {
  PropertyDescriptor pd2 = new PropertyDescriptor(propertyName, pt1.getClass());
  Method methodSetX = pd2.getWriteMethod(); methodSetX.invoke(pt1, value);
 }

 private static PropertyDescriptor getPropert(Object pt1,String propertyName) throws IntrospectionException,
   IllegalAccessException, InvocationTargetException {
  PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt1.getClass());  //参数为“属性名”“类名”
  Method methodGetY = pd.getReadMethod();       //把PropertyDescriptor类的其中一个属性getReadMethod  赋值给一个变量
  Object retVal = methodGetY.invoke(pt1);
  return pd;
 }
}

你可能感兴趣的:(String,object,Class,import)