package test;
import java.io.InputStream;
import java.util.List;
public class Person {
public String name="jkxqj";
private int password=123;
private static int age=21;
public Person(){
System.out.println("person!");
}
public Person(String name){
System.out.println(name);
}
public Person(String name,int password){
System.out.println(name+":"+password);
}
private Person(List arraylist){
System.out.println("list!");
}
public void aa(String name,int password){
System.out.println(name+":"+password);
}
public Class[] aa(String name,int[] password){
return new Class[]{String.class};
}
private void aa(InputStream in){
System.out.println(in);
}
public static void aa(int num){
System.out.println(num);
}
public static void main(String[] args){
System.out.println("main!");
}
}
package test;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
//反射类的构造函数,创建类的对象
public class testPerson {
//反射构造函数:public Person()
@Test
public void test1() throws Exception{
Class clazz=Class.forName("test.Person");
Constructor c=clazz.getConstructor(null);
Person p= (Person)c.newInstance(null);
System.out.println(p.name);
}
//反射构造函数:public Person(String name)
@Test
public void test2() throws Exception{
Class clazz=Class.forName("test.Person");
Constructor c=clazz.getConstructor(String.class);
Person p= (Person)c.newInstance("XXX");
System.out.println(p.name);
}
//反射构造函数:public Person(String name,int password)
@Test
public void test3() throws Exception{
Class clazz=Class.forName("test.Person");
Constructor c=clazz.getConstructor(String.class,int.class); //返回指定参数的构造函数,仅public
Person p= (Person)c.newInstance("OOO",007);
System.out.println(p.name);
}
//反射构造函数:private Person(List list)
@Test
public void test4() throws Exception{
Class clazz=Class.forName("test.Person");
Constructor c=clazz.getDeclaredConstructor(List.class);//返回指定参数的构造函数,私有公有都可以
c.setAccessible(true); //暴力反射
//私有成员一般不可访问,但是反射通过这个方法可以强制访问私有成员
Person p= (Person)c.newInstance(new ArrayList());
System.out.println(p.name);
}
//创建对象的另外一种途径:以下代码等效于test1()
@Test
public void test5() throws Exception{
Class clazz=Class.forName("test.Person");
Person p= (Person)clazz.newInstance();
System.out.println(p.name);
}
//反射类的方法:public void aa(String name,int password)
@Test
public void test6() throws Exception{
Person p= new Person();
Class clazz=Class.forName("test.Person");
Method method=clazz.getMethod("aa",String.class,int.class);
method.invoke(p,"jinkai",21);
}
//反射类的方法:public Class[] aa(String name,int[] password)
@Test
public void test7() throws Exception{
Person p= new Person();
Class clazz=Class.forName("test.Person");
Method method=clazz.getMethod("aa",String.class,int[].class);
Class cs[]=(Class[])method.invoke(p,"aaa",new int[]{1,2,3});
System.out.println(cs[0]);
}
//反射类的方法: private void aa(InputStream in)
@Test
public void test8() throws Exception{
Person p= new Person();
Class clazz=Class.forName("test.Person");
Method method=clazz.getDeclaredMethod("aa",InputStream.class); //获取私有
method.setAccessible(true);
method.invoke(p,new FileInputStream("C:\\1.txt"));//需要在C盘根目录新建一个1.txt
}
//反射类的方法: private static void aa(int num)
@Test
public void test9() throws Exception {
//Person p= new Person(); 静态方法调用不需要对象
Class clazz=Class.forName("test.Person");
Method method=clazz.getMethod("aa",int.class);
method.invoke(null,22);
}
//反射类的方法: private static void main(String[] args)
@Test
public void test10() throws Exception {
Class clazz=Class.forName("test.Person");
Method method=clazz.getMethod("main",String[].class);
method.invoke(null,new Object[]{new String[]{"aa","bb"}});//main(String s1, String s2)
//或者 method.invoke(null,(Object)new String[]{"aa","bb"});//main(String s1, String s2)
//jdk1.5及以上 method.invoke(Object obj,Object...args)
//jdk1.4 method.invoke(Object obj[],Object...args)
}
//反射字段:public String name="jkxqj";
@Test
public void test11() throws Exception {
Person p= new Person();
Class clazz=Class.forName("test.Person");
Field f=clazz.getField("name");
//获取字段值
Object value=(String)f.get(p);
//返回字段类型
Class type=f.getType();
if(type.equals(String.class)){
String svalue=(String)value;
System.out.println(svalue);
}
//设置字段值
f.set(p,"XX");
System.out.println(p.name);
}
//反射字段:private int password;
@Test
public void test12() throws Exception {
Person p= new Person();
Class clazz=Class.forName("test.Person");
Field f=clazz.getDeclaredField("password");//获取私有字段
f.setAccessible(true);
//获取字段值
System.out.println(f.get(p));
}
//反射字段: private static int age=21;
@Test
public void test13() throws Exception {
Class clazz=Class.forName("test.Person");
Field f=clazz.getDeclaredField("age");//获取私有字段
f.setAccessible(true);
System.out.println(f.get(null));
}
}
Java反射,布布扣,bubuko.com
Java反射