package com.fly.reflect;
import java.io.InputStream;
import java.util.List;
/*
* Person类,用于反射构造方法
*/
public class Person {
public String name = "aa";
private int password = 1232323;
private static int age = 19;
public Person() {
System.out.println("person");
}
public Person(String name) {
System.out.println("name:" + name);
}
public Person(String name, int age) {
System.out.println("person name:" + name + " age:" + age);
}
private Person(List list) {
System.out.println("list");
}
private Person(int a) {
System.out.println("a==" + a);
}
//测试用
public void testMe() {
System.out.println("可以了");
}
public void aa1() {
System.out.println("aa1");
}
public void aa1(String name, int password) {
System.out.println(name + ":" + password);
}
public Class[] aa1(String name, int[] password) {
return new Class[]{String.class};
}
private void aa1(InputStream in) {
System.out.println(in);
}
public static void aa1(int num) {
System.out.println(num);
}
public static void main(String[] args) {
System.out.println("This is main function");
}
}
package com.fly.reflect;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
//反射类的构造函数,创建类的对象
public class Demo1 {
//反射构造函数:public Person()
@Test
public void test1() throws Exception {
//加载类
Class clazz = Class.forName("com.fly.reflect.Person");
//获取空参构造方法
Constructor c = clazz.getConstructor(null);
//创建对象
Person p = (Person)c.newInstance(null);
p.testMe();
}
//反射构造函数:public Person(String name)
@Test
public void test2() throws Exception {
Class clazz = Class.forName("com.fly.reflect.Person");
Constructor c = clazz.getConstructor(String.class);
Person p = (Person)c.newInstance("gaozx");
p.testMe();
}
//反射构造函数:public Person(String name, int age)
@Test
public void test3() throws Exception {
Class clazz = Class.forName("com.fly.reflect.Person");
Constructor c = clazz.getConstructor(String.class, int.class);
Person p = (Person)c.newInstance("gaozx", 12);
p.testMe();
}
//反射构造函数:public Person(List list)
@Test
public void test4() throws Exception {
Class clazz = Class.forName("com.fly.reflect.Person");
//获取私有构造方法
Constructor c = clazz.getDeclaredConstructor(List.class);
c.setAccessible(true); //暴力反射
Person p = (Person)c.newInstance(new ArrayList());
p.testMe();
}
//getDeclaredConstrctor和getConstructor的区别
@Test
public void test5() throws Exception {
Class clazz = Class.forName("com.fly.reflect.Person");
Constructor c = clazz.getConstructor(String.class);
Person p = (Person)c.newInstance("Sim");
p.testMe();
}
}
package com.fly.reflect;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Method;
import org.junit.Test;
//反射类的方法
public class Demo2 {
//反射类的方法:public void aa1()
@Test
public void test1() throws Exception {
Person p = new Person();
Class clazz = Class.forName("com.fly.reflect.Person");
Method method = clazz.getMethod("aa1", null);
method.invoke(p, null);
}
//反射类的方法:public void aa1(String name, int password)
@Test
public void test2() throws Exception {
Person p = new Person();
Class clazz = Class.forName("com.fly.reflect.Person");
Method method = clazz.getMethod("aa1", String.class, int.class);
method.invoke(p, "aa", 1);
}
//反射类的方法:public Class[] aa1(String name, int[] password)
@Test
public void test3() throws Exception {
Person p = new Person();
Class clazz = Class.forName("com.fly.reflect.Person");
Method method = clazz.getMethod("aa1", String.class, int[].class);
Class[] cs = (Class[]) method.invoke(p, "aa", new int[]{1, 2});
System.out.println(cs[0]);
}
//反射类的方法:private void aa1(InputStream in)
@Test
public void test4() throws Exception {
Person p = new Person();
Class clazz = Class.forName("com.fly.reflect.Person");
Method method = clazz.getDeclaredMethod("aa1", InputStream.class);
method.setAccessible(true);
method.invoke(p, new FileInputStream("e:\\1.txt"));
}
//反射类的方法:public static void aa1(int num)
@Test
public void test5() throws Exception {
Person p = new Person();
Class clazz = Class.forName("com.fly.reflect.Person");
Method method = clazz.getMethod("aa1", int.class);
method.invoke(null, 11);
}
//反射类的方法:public static void main(String[] args)
@Test
public void test6() throws Exception {
Class clazz = Class.forName("com.fly.reflect.Person");
Method method = clazz.getMethod("main", String[].class);
method.invoke(null, new Object[]{new String[]{"1","2"}});
}
}
package com.fly.reflect;
import java.lang.reflect.Field;
import org.junit.Test;
//反射类的字段
public class Demo3 {
//反射字段:public String name = "aa";
@Test
public void test1() throws Exception {
Person p = new Person();
Class clazz = Class.forName("com.fly.reflect.Person");
Field f = clazz.getField("name");
//获取字段的类型
Class type = f.getType();
//获取字段的值
Object obj = (String) f.get(p);
System.out.println((String)obj);
f.set(p, "bbbb");
System.out.println(p.name);
}
//反射字段:private int password;
@Test
public void test2() throws Exception {
Person p = new Person();
Class clazz = Class.forName("com.fly.reflect.Person");
Field f = clazz.getDeclaredField("password");
f.setAccessible(true);
int pwd = (Integer) f.get(p);
System.out.println(pwd);
f.set(p, 999999);
int pwd1 = (Integer) f.get(p);
System.out.println(pwd1);
}
//反射字段:private static int age = 19
@Test
public void test3() throws Exception {
Person p = new Person();
Class clazz = Class.forName("com.fly.reflect.Person");
Field f = clazz.getDeclaredField("password");
f.setAccessible(true);
int pwd = (Integer) f.get(p);
System.out.println(pwd);
f.set(p, 999999);
int pwd1 = (Integer) f.get(p);
System.out.println(pwd1);
}
}
本文详细介绍了Java反射机制的应用,通过具体示例展示了如何使用反射来获取类的构造方法、方法和字段等内容。从加载类到获取不同类型的构造方法,再到调用类的方法及修改类的字段,全面解析反射技术。
617

被折叠的 条评论
为什么被折叠?



