反射reflect
需要用到的类
package com.xmjpw.reflectone;
import java.lang.reflect.Method;
public class ReflectOne { }
class Type {
public static void main(String[] args) {
Class<?> classType5=ExtendType.class;
Method[] methods=classType5.getMethods();//获取公开的方法
for (Method m : methods) {
System.out.println(m);
}
}
public int pubIntField;
public String pubStringField;
private int priIntField;
public Type() {
//System.out.println("Type类的无参构造器");
Log("Default Constructor");
}
Type(int a, String b) {
pubIntField = 1;
pubStringField = b;
//System.out.println("Type的有参数ab构造器");
Log("Constructor with parameters");
}
public void setIntField(int c) {
this.priIntField = c;
}
public int getIntField() {
return priIntField;
}
private void Log(String msg) {
System.out.println("参数为msg的Log方法:" + msg);
}
}
class ExtendType extends Type {
public int pubIntExtendField;
public String pubStringExtendField;
private int priIntExtendField;
public ExtendType() {
System.out.println("ExtendType的无参构造器");
Log("ExtendType");
}
ExtendType(int a, String b) {
pubIntExtendField = a;
pubStringExtendField = b;
System.out.println("ExtendType的有参数ab的构造器!");
Log("Constructor with parameters");
}
public void setIntExtendField(int c) {
this.priIntExtendField = c;
}
public int getIntExtendField() {
return priIntExtendField;
}
private void Log(String msg) {
System.out.println("参数为msg的Log方法:" + msg);
}
}
反射demo
package com.xmjpw.reflectone;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class ReflectTwo {
public static void main(String[] args) throws NoSuchMethodException,
SecurityException, ClassNotFoundException, InstantiationException,
IllegalAccessException, IllegalArgumentException,
InvocationTargetException, NoSuchFieldException {
System.out.println("----------------------获取类的Class对象");
String var1 = "我的对象是String";
Class<?> classType = var1.getClass();
System.out.println(classType);
Class<?> classType2 = String.class;//运用.class语法
System.out.println(classType2);
Class<?> classType3 = String.class;
System.out.println(classType3);
classType3 = Class.forName("java.lang.String");
System.out.println(classType3);
System.out.println("-------------------------获取类的Fields");
Class<?> classType4 = ExtendType.class;
Field[] fields = classType4.getFields();
for (Field f : fields) {
System.out.println(f);
}
System.out.println("-------------------获取类的Method");
Class<?> classType5 = ExtendType.class;
Method[] methods = classType5.getDeclaredMethods();// 获取公开的方法
for (Method m : methods){
System.out.println(m);
}
Constructor<?>[] constructors = classType5.getConstructors();
for (Constructor<?> c : constructors){
System.out.println(c);
}
System.out.println("------------------新建类的实例");
Class<?> cone = ExtendType.class;
Object inst1;
try {
inst1 = cone.newInstance();
System.out.println(inst1);
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println();
// 调用默认Constructor对象的newInstance方法;
Class<?> classType6 = ExtendType.class;
Constructor<?> cons2 = classType6.getConstructor();
Object inst2 = cons2.newInstance();
System.out.println(inst2);
System.out.println("调用带参数的Constructor对象的newInstance方法");
Constructor<?> con3 = classType6.getDeclaredConstructor(int.class,
String.class);
Object inst3 = con3.newInstance(1, "123");
System.out.println(inst3);
System.out.println();
Class<?> classType7 = ExtendType.class;
Object inst4 = classType7.newInstance();
Method logMethod = classType7.getDeclaredMethod("Log", String.class);
logMethod.setAccessible(true);
logMethod.invoke(inst4, "test");
System.out.println("------------------------设置/获取类的属性值");
Class<?> classType8 = ExtendType.class;
Object inst5 = classType8.newInstance();
System.out.println("-----------------");
Field intField = classType8.getField("pubIntExtendField");
intField.setInt(inst5, 100);
int value = intField.getInt(inst5);
}
}
效果图展示:
三个反射包中的类:
Constructor:代表类的单个构造方法,通过Constructor我们可执行一个类的某个构造方法(有参或者无参)来创建对象时。
Method:代表类中的单个方法,可以用于执行类的某个普通方法,有参或无参,并可以接收返回值。
Field:代表类中的单个属性,用于set或get属性
AccessibleObject:以上三个类的父类,提供了构造方法,普通方法,和属性的访问控制的能力。
使用Class类中的方法可以获得该类中的所有Constructor对象,Method对象,和Field对象。但是任然无法访问私有化的构造方法,普通方法,和私有属性,此时我们可以使用他们继承父类(AccessibleObject)中的setAccessible()方法,来设置或取消访问检查,以达到访问私有对象的目的。