//加载类_1
Class class_0 = Class.forName("com.reflextest.ReflexTest");
//加载类_2
Class class_1 = new ReflexTest().getClass();
//加载类_3
Class class_2 = ReflexTest.class;
步骤
Class.forName()、getClass()、className.class;
class.getConstructor();
constructor.new Instance();
实例代码
// 无参构造器
@Test
public void test() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
//反射构造器
Class class_ = Class.forName("com.reflextest.ReflexTest");
//获取Constructor对象
Constructor constructor = class_.getConstructor(null);
//创建类对象
ReflexTest reflexTest = (ReflexTest) constructor.newInstance(null);
//调用方法
reflexTest.print();
}
// 带有一个参数的构造器
@Test
public void test_() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
//反射构造器
Class class_ = Class.forName("com.reflextest.ReflexTest");
//获取Constructor对象
Constructor constructor = class_.getConstructor(String.class);
//创建类对象
ReflexTest reflexTest = (ReflexTest) constructor.newInstance("欧阳铁柱");
//调用方法
reflexTest.print();
}
// 带有多个参数的构造器
@Test
public void test_1() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
//反射构造器
Class class_ = Class.forName("com.reflextest.ReflexTest");
//获取Constructor对象 --- 只能加载public类型的构造器
Constructor constructor = class_.getConstructor(String.class, Integer.class);
//创建类对象
ReflexTest reflexTest = (ReflexTest) constructor.newInstance("上官翠花", 5);
//调用方法
reflexTest.print();
}
// 私有构造器
@Test
public void test_2() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
//反射构造器
Class class_ = Class.forName("com.reflextest.ReflexTest");
//获取私有构造器对象 ---- 使用getdDclaredConstructor()方法获取私有构造器
Constructor constructor = class_.getDeclaredConstructor(int.class);
//使用暴力反射实现访问私有构造器
constructor.setAccessible(true);
//创建类对象
ReflexTest reflexTest = (ReflexTest) constructor.newInstance(1);
//调用方法
reflexTest.print();
}
使用Class对象直接创建反射类对象
// 直接使用Class对象创建反射对象
@Test
public void test_3() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
//创建Class对象
Class class_ = Class.forName("com.reflextest.ReflexTest");
//使用Class对象创建反射类对象,但是是使用无参构造函数创建对象(为什么要在定义有参构造函数后还有定义有参构造函数)
ReflexTest reflexTest = (ReflexTest) class_.newInstance();
//调用方法
reflexTest.print();
}
步骤
Class.forName()、getClass()、className.class;
clazz.getMethod() ---- get public method
clazz.getDeclaredMethod() ---- get private method
method.invoke(Object obj, args);
实例代码
public class MainTest {
/**
* 反射
*/
//实现反射方法
/**
* 反射 public void print(){}
*/
@Test
public void testMethod_0() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
//加载类
Class clazz = ReflexTest.class;
//获取Method对象
Method method = clazz.getMethod("print", null);
// method.invoke((ReflexTest)(clazz.newInstance()), null);
/**
* invoke:
* obj - 从中调用底层方法的对象
* args - 用于方法调用的参数 (可变长参数)
*/
method.invoke(new ReflexTest(), null);
}
/**
* public void print(String s){}
*/
@Test
public void testMethod_1() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
//加载类
Class clazz = Class.forName("com.reflextest.ReflexTest");
//获取Method对象
Method method = clazz.getMethod("print", String.class);
//执行方法
method.invoke(new ReflexTest(), "欧阳铁柱");
}
/**
* public void print(String[] s){}
*/
@Test
public void testMethod_2() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
//加载类
Class clazz = Class.forName("com.reflextest.ReflexTest");
//获取Method对象
Method method = clazz.getMethod("print", String[].class);
//执行方法
method.invoke(new ReflexTest(), (Object)(new String[]{"欧阳铁柱", "上官翠花", "孙秃子", "李狗蛋"}));
}
/**
* public Class[] print(String[] s, int a){}
*/
@Test
public void testMethod_3() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
//获取Class对象
Class clazz = Class.forName("com.reflextest.ReflexTest");
//获取Method对象
Method method = clazz.getMethod("print", String[].class, int.class);
//执行方法
System.out.println(
method.invoke(new ReflexTest(), (Object)(new String[]{"欧阳铁柱", "上官翠花", "孙秃子", "李狗蛋"}),
4));
}
/**
* private void print(FileInputStream in){}
*/
@Test
public void testMethod_4() throws ClassNotFoundException, NoSuchMethodException, FileNotFoundException, InvocationTargetException, IllegalAccessException {
//获取Class对象
Class clazz = Class.forName("com.reflextest.ReflexTest");
//获取Method对象 --- 使用getDeclareMethod()方法获取私有方法
Method method = clazz.getDeclaredMethod("print", FileInputStream.class);
//暴力反射
method.setAccessible(true);
//执行方法
method.invoke(new ReflexTest(), new FileInputStream(new File("README.md")));
}
/**
* public static void print(int n){}
*/
@Test
public void testMethod_5() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
//创建Class对象
Class clazz = Class.forName("com.reflextest.ReflexTest");
//获取Method对象
Method method = clazz.getMethod("print", int.class);
//执行方法 --- 静态方法执行不需要对象,对象不必要写
method.invoke(new ReflexTest(), 1);
method.invoke(null, 1);
}
/**
* 反射main方法
*/
@Test
public void reflexMainFunc() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
//加载类
Class clazz = Class.forName("com.reflextest.ReflexTest");
//获取Method对象
Method method = clazz.getMethod("main", String[].class);
//执行main方法,传递参数(Object)new String[]{"value"}
method.invoke(null, (Object)new String[]{"a"});
}
}
步骤
Class.forName()、getClass()、className.class;
clazz.getField()、clazz.getDeclaredField()[获取静态属性]
实例代码
public class MainTest {
/**
* 实现反射字段
*/
@Test
public void testReflexValue_0() throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
// public int value;
//加载类
Class clazz = Class.forName("com.reflextest.ReflexTest");
//获取Field对象
Field field = clazz.getField("value");
//使用字段
ReflexTest r = new ReflexTest();
field.set(r, (int)5);
//获取值
System.out.println(field.get(r));
//判断是否是int类型的值
System.out.println(field.getType().equals(int.class));
//获取int值
System.out.println(field.getInt(r));
}
@Test
public void testReflexValue_1() throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
// private String str;
//加载类
Class clazz = Class.forName("com.reflextest.ReflexTest");
//获取Field对象
Field field = clazz.getDeclaredField("str");
//暴力反射
field.setAccessible(true);
//使用字段
ReflexTest r = new ReflexTest();
field.set(r, "HelloWorld");
//获取值
System.out.println(field.get(r));
//判断是否是int类型的值
System.out.println(field.getType().equals(int.class));
}
@Test
public void testReflexValue_2() throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
//private static int value_1;
//加载类
Class clazz = Class.forName("com.reflextest.ReflexTest");
//获取Field对象
Field field = clazz.getDeclaredField("value_1");
//暴力反射
field.setAccessible(true);
//使用字段
field.set(null, 1);
//获取值
System.out.println(field.get(null));
System.out.println(field.getInt(null));
//判断是否是int类型的值
System.out.println(field.getType().equals(int.class));
}
}
/**
* 反射测试类
*/
class ReflexTest{
/* ********************************************属性(字段)************************************************** */
//public 字段
public int value;
private String str;
private static int value_1;
/* ********************************************构造器************************************************** */
//无参构造器
public ReflexTest() {
System.out.println("构造器ReflexTest()");
}
//带有一个参数的构造器
public ReflexTest(String str) {
System.out.println(" 传入参数:" + str);
}
//带有多个参数的构造器
public ReflexTest(String str, Integer v) {
System.out.println(" 传入参数:" + str + " " + v);
}
//私有构造器
private ReflexTest(int v){
System.out.println("私有构造器,传入的参数:" + v);
}
/* ********************************************方法************************************************** */
public void print(){
System.out.println("HelloWorld");
}
public void print(String s){
System.out.println("含参的print()方法接收的参数:" + s);
}
public void print(String[] s){
System.out.println("含参的print()方法接收的参数:" );
for (String s1:s
) {
System.out.println(s1);
}
}
public Class[] print(String[] s, int a){
return new Class[]{String.class};
}
private void print(FileInputStream in){
System.out.println(in.toString());
}
public static void print(int n){
System.out.println("静态print()方法传入的参数是:" + n);
}
public static void main(String[] args) {
System.out.println("这是一个main方法");
}
}