黑马程序员_java学习日记num15

来源:互联网 发布:西部网络达人秀开场舞 编辑:程序博客网 时间:2024/06/09 23:24
------java培训,android培训,期待与你交流---------------
一、反射概述
1,java程序中的各个java类属于同一类事物,描述这类事物的java类名就是class
所谓的反射就是将java类中的各个类型的成员等映射为对应的类
2,如果得到各个字节码自己对应的实例对象(class类型)
类名.class  例如:Person.class
Class.forName("java.long.String")
对象.getClass();

二,Constructor类
1,Constructor类代表某个类中的一个构造方法
2,取得构造方法
得到某个类所有的构造方法
Constructor[] constructor = Class.forName("java.long.String").getConstructors();
得到某个类的某个构造方法
Constructor constructor = Class.forName("java.long.String").getConstructor();
3,创建实例
Sring str = (String)constructor.newInstance(new StringBuilder("hello,itcast");

4,示例:
ReflectClass rf = new ReflectClass(54, 66);
Constructor constructor = rf.getClass().getConstructor(int.class,int.class);
ReflectClass Test = (ReflectClass)constructor.newInstance(5,6);

三、Field类
Field类代表成员中某个变量。
对于类中非公共的变量,也可以访问,不过需要用到setAccessible(true)方法
示例如下:
Field[] fields = rf.getClass().getDeclaredFields();
for(Field field:fields){
if(field.getType() == String.class){
System.out.println(field.getName());
field.setAccessible(true);
String oldValue = (String)field.get(rf);
System.out.println(oldValue);
String newValue = oldValue.replace('a', 'g');
field.set(rf, newValue);
System.out.println(newValue);
}
System.out.println(field.toString());
}

四,Method类
Method类 代表某个类中的一个成员方法
得到某一个方法:Method method = Class.forName("java.lang.String").getMethod("charAt",int.class);
调用该方法:method.invoke(str,1);
示例:
String str = "xyz";
Method method = Class.forName("java.lang.String").getMethod("charAt", int.class);
method.invoke(str, 2);
System.out.println(method.invoke(str, 2));

五、内省到JavaBean

内省(IntroSpector)是Java 语言对 Bean 类属性、事件的一种缺省处理方法。例如Person类中有属性 name, 那我们可以通过getName/setName 来1取值或者设置新的值。存在这种通过get和set方法操作成员属性的类可以叫做JavaBean类,当然我们也可以当做简单的类使用。

JavaBean是一种特殊的Java类,主要用于传递数据信息,这种java类中的方法主要用于访问私有的字段,且方法名符合某种命名规则。JavaBean的实例对象通常称之为值对象(Value Object,简称VO)。一个类被当作javaBean使用时,JavaBean的属性是根据方法名推断出来的,它根本看不到java类内部的成员变量。去掉set前缀,然后取剩余部分,如果剩余部分的第二个字母是小写的,则把剩余部分的首字母改成小写,这样得到的就是javaBean的属性。如:getName的属性名是name ,getCPU的属性名是CPU。Java 中提供了一套 API 用来访问某个属性的 getter/setter 方法,这套API就称为内省。他们存在于java.beans包中。

可以通过两种方式来实现功能:代码如下
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class IntroSpectorDemo {

public static void main(String[] args) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
String propertyName = "a";
ReflectClass rf = new ReflectClass(44,55);
getPropertyValue(propertyName, rf);
   setPropertyValue(propertyName, rf,55555);
   
   
   BeanInfo beanInfo = Introspector.getBeanInfo(rf.getClass());//通过内省获得BeanInfo的子类
   PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();//通过BeanInfo得到属性的描述
   for(PropertyDescriptor pd:pds){
    if(pd.getName().equals(propertyName)){//判断属性
    Method methodGetA = pd.getReadMethod();
    System.out.println(methodGetA.invoke(rf));
    break;
    }
   }
}

public static void setPropertyValue(String propertyName, ReflectClass rf,int value)
throws IntrospectionException, IllegalAccessException,
InvocationTargetException {
PropertyDescriptor pd = new PropertyDescriptor(propertyName,rf.getClass());
   Method methodSetA = pd.getWriteMethod();
   methodSetA.invoke(rf, value);
   System.out.println(rf.getA());
}

public static void getPropertyValue(String propertyName, ReflectClass rf)
throws IntrospectionException, IllegalAccessException,
InvocationTargetException {
PropertyDescriptor pd = new PropertyDescriptor(propertyName,rf.getClass());
Method methodGetA = pd.getReadMethod();
Object retVal = methodGetA.invoke(rf);
System.out.println(retVal);
}



}
0 0
原创粉丝点击