public class Person {
public String name="aaa";
private int password;
private static int age;
public Person() {
System.out.println("person");
}
public Person(String name){
System.out.println("person name:"+name);
}
public Person(String name,int password){
System.out.println("person name password:"+name+":"+password);
}
private Person(List<String> list){
System.out.println("list");
}
public void aa1(){
System.out.println("aa1");
}
public void aa1(String[] strs,String b){
for (String string : strs) {
System.out.println(string);
}
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 class Demo1 {
/**
* 反射加载类
* @throws Exception
*/
@Test
public void test1() throws Exception{
//1
Class clazz=Class.forName("com.tls.reflect.Person");
//2
Class clazz1 = new Person().getClass();
//3
Class clazz2 = Person.class;
}
}
/**
* 反射类的构造函数,创建类的对象
*
*/
public class Demo2 {
@Test//反射出public Person()
public void test1() throws Exception {
Class clazz = Class.forName("com.tls.reflect.Person");
Constructor c = clazz.getConstructor(null);
Person p = (Person)c.newInstance(null);
System.out.println(p.name);
}
@Test//public Person(String name)
public void test2() throws Exception {
Class clazz = Class.forName("com.tls.reflect.Person");
Constructor c = clazz.getConstructor(String.class);
Person p = (Person)c.newInstance("tls");
System.out.println(p.name);
}
@Test//public Person(String name,int password)
public void test3() throws Exception {
Class clazz = Class.forName("com.tls.reflect.Person");
Constructor c = clazz.getConstructor(String.class,int.class);
Person p = (Person)c.newInstance("tls",12);
System.out.println(p.name);
}
@Test//private Person(List<String> list)
public void test4() throws Exception {
Class clazz = Class.forName("com.tls.reflect.Person");
Constructor c = clazz.getDeclaredConstructor(List.class);
c.setAccessible(true);//暴力反射
Person p = (Person)c.newInstance(new ArrayList<>());
System.out.println(p.name);
}
@Test//创建对象的另一种途径:相当于上面test1方法
public void test5() throws Exception {
Class clazz = Class.forName("com.tls.reflect.Person");
Person p = (Person)clazz.newInstance();
System.out.println(p.name);
}
}
public class Demo3 {
Class clazz=null;
Person p=null;
@Before
public void before() throws Exception{
clazz=Class.forName("com.tls.reflect.Person");
p=new Person();
}
@Test//反射类的方法:public void aa1()
public void test1() throws Exception{
Method method=clazz.getMethod("aa1", null);
method.invoke(p, null);//第一个参数为对象,表示调用哪个对象的方法
}
@Test//反射类的方法:public void aa1(String name,int password)
public void test2() throws Exception{
Method method=clazz.getMethod("aa1", String.class,int.class);
method.invoke(p, "tls",12);//第一个参数为对象,表示调用哪个对象的方法
}
@Test//反射类的方法:public void aa1(String[] strs)
public void test12() throws Exception{
Method method=clazz.getMethod("aa1",String[].class,String.class);//这里没有问题,单独的数组作为参数就会有问题
method.invoke(p,new String[]{"1","a"}, "b");
}
@Test//反射类的方法:public Class[] aa1(String name,int[] password)
public void test3() throws Exception{
Method method=clazz.getMethod("aa1", String.class,int[].class);
Class[] cs=(Class[])method.invoke(p, "tls",new int[]{1,2});//第一个参数为对象,表示调用哪个对象的方法
System.out.println(cs[0]);
}
@Test//反射类的方法:private void aa1(InputStream in)
public void test4() throws Exception{
Method method=clazz.getDeclaredMethod("aa1", InputStream.class);
method.setAccessible(true);
method.invoke(p, new FileInputStream(new File("c:\\1.txt")));//第一个参数为对象,表示调用哪个对象的方法,这里c盘下要有该文件,不然报错
}
@Test//反射类的方法:public static void aa1(int num)
public void test5() throws Exception{
Method method=clazz.getMethod("aa1", int.class);
method.invoke(null, 12);//这里不需要对象,因为是静态方法
}
@Test//反射类的main方法:public static void main(String[] args)
public void test6() throws Exception{
Method method=clazz.getMethod("main", String[].class);
// method.invoke(null, new String[]{"1","2"});//这样是不对的
method.invoke(null, new Object[]{new String[]{"1","2"}});//为了拆分后还能执行,将参数包装成object数组
method.invoke(null, (Object)new String[]{"1","2"});//为了防止认为是数组,这里强转成object也可以
//jdk1.5 Method.invoke(obj,Object ...args)
//jdk1.5 Method.invoke(obj,Object[] args) a(string name,string password)
//jdk1.4 Method.invoke(obj,new Object[]{"aa","bbb"})//这里会将object数组拆分之后作为参数传入
}
public void after() throws Exception{
clazz=null;
p=null;
}
}
/**
* 反射字段
*
*/
public class Demo4 {
Class clazz=null;
Person p=null;
@Before
public void before() throws Exception{
clazz=Class.forName("com.tls.reflect.Person");
p=new Person();
}
@Test//反射类的字段:public String name
public void test1() throws Exception{
Field field = clazz.getField("name");
Class type = field.getType();
if(type.getName() instanceof String){
//获取字段值
String string=(String)field.get(p);
System.out.println(string);
}
//设置字段值
field.set(p, "dddd");
System.out.println(p.name);
}
@After
public void after() throws Exception{
clazz=null;
p=null;
}
}