反射
一个类有多个组成部分,例如:成员变量,方法,构造方法等。反射就是加载类、并解剖出类的各个组成部分。
如果为私有的需要declared
Class clazz=Class.forName("com.itcast.refelect.Person");
Constructor c=clazz.getConstructor(String.class);
Person p=(Person) c.newInstance("aaa");
System.out.println(p.name);
//getConstructor只能获得public类型的构造函数,如果想好的private的构造函数需要用getDeclaredConstructor()
//私有的只有内部可以访问,但是反射可以setAccessible(true)暴力反射
例子:
1,
Peroson.java
package com.itcast.refelect;
import java.util.List;
public class Person {
public String name="aaa";
public Person(){
System.out.println("person");
}
public Person(String name){
System.out.println("person name");
}
public Person(String name,int password){
System.out.println("person name password");
}
private Person(List list){
System.out.println("list");
}
}
2,测试类
package com.itcast.refelect;
import java.util.List;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import org.junit.Test;
import com.itcast.refelect.Person;
//反射类的构造函数,创建类的对象 getConstructor()
public class gouzaohanshu {
@Test
public void test1()throws Exception{
//1,加载类
Class clazz = Class.forName("com.itcast.refelect.Person");
//2,获取构造方法
Constructor c = clazz.getConstructor(null);
//3,实例化
Person p =(Person)c.newInstance(null);
System.out.println(p.name);
}
@Test
public void test2()throws Exception{
Class clazz=Class.forName("com.itcast.refelect.Person");
Constructor c=clazz.getConstructor(String.class);
Person p=(Person) c.newInstance("aaa");
System.out.println(p.name);
}
@Test
public void test3()throws Exception{
Class clazz=Class.forName("com.itcast.refelect.Person");
Constructor c=clazz.getConstructor(String.class,int.class);
Person p=(Person) c.newInstance("bbb",2);
System.out.println(p.name);
}
@Test
public void test4()throws Exception{
Class clazz=Class.forName("com.itcast.refelect.Person");
Constructor c=clazz.getDeclaredConstructor(List.class);
//暴力反射
c.setAccessible(true);
Person p=(Person)c.newInstance(new ArrayList());
System.out.println(p.name);
}
}
2,反射方法Method getMethod()
Person.java
package com.itcast.refelect;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
public class Person {
/*加载类获得类的字节码*/
/* public static void main(String[] args) throws ClassNotFoundException{
//1
Class clazz1 = Class.forName("com.itcast.refelect.Person");
//2
Class clazz2 = new Person().getClass();
//3
Class clazz3 = Person.class;
}*/
public String name="aaa";
public Person(){
System.out.println("person");
}
public Person(String name){
System.out.println("person name");
}
public Person(String name,int password){
System.out.println("person name password");
}
private Person(List list){
System.out.println("list");
}
public void aa(){
System.out.println("aa");
}
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(FileInputStream in){
System.out.println(in);
}
public static void aa(int num){
System.out.println(num);
}
}
fanshefangfa.java
package com.itcast.refelect;
import java.io.FileInputStream;
import java.lang.reflect.Method;
import org.junit.Test;
//反射类的方法
public class fanshefangfa {
@Test
public void test1()throws Exception{
Person p=new Person();
Class clazz=Class.forName("com.itcast.refelect.Person");
Method method=clazz.getMethod("aa", null);
method.invoke(p, null);
}
//public void aa(String name,int password)
@Test
public void test2()throws Exception{
Person p=new Person();
Class clazz=Class.forName("com.itcast.refelect.Person");
Method method=clazz.getMethod("aa", String.class,int.class);
method.invoke(p, "zhangsan",21);
}
//public Class[] aa(String name,int[] password)
@Test
public void test3()throws Exception{
Person p=new Person();
Class clazz=Class.forName("com.itcast.refelect.Person");
Method method=clazz.getMethod("aa", String.class,int[].class);
Class cs[]=(Class[]) method.invoke(p, "abc",new int[]{1,2,3});
System.out.println(cs[0]);
}
//private void aa(FileInputStream in)
@Test
public void test4()throws Exception{
Person p=new Person();
Class clazz=Class.forName("com.itcast.refelect.Person");
Method method=clazz.getDeclaredMethod("aa", FileInputStream.class);
method.setAccessible(true);
method.invoke(p,new FileInputStream("d:\\test.txt"));
}
//public static void aa(int num)
@Test
public void test5()throws Exception{
Class clazz=Class.forName("com.itcast.refelect.Person");
Method method=clazz.getMethod("aa", int.class);
method.invoke(null, 16);
}
}
7,main方法的反射
@Test
public void test6()throws Exception{
Class clazz=Class.forName("com.itcast.refelect.Person");
Method method=clazz.getMethod("main", String[].class);
//反射main需要骗jvm可以用method.invoke(null,(Object)new String[]{"aaa","bbb"});
//也可以method.invoke(null,new Object[]{new String[]{"aaa","bbb"}});
method.invoke(null,(Object)new String[]{"aaa","bbb"});
}
8,反射字段注意私有
getField()获得字段
getType()获得字段类型
package com.itcast.refelect;
import java.lang.reflect.Field;
import org.junit.Test;
//反射字段
public class fansheziduan {
//public String name="aaa";
@Test
public void test()throws Exception{
Person p=new Person();
Class clazz=Class.forName("com.itcast.refelect.Person");
Field f=clazz.getField("name");
//获取字段的值
Object value=f.get(p);
//获取字段的类型
Class type=f.getType();
if(type.equals(String.class)){
String svalue=(String)value;
System.out.println(svalue);
}
f.set(p, "xxxx");
System.out.println(p.name);
}
@Test
//private int password;
public void test2()throws Exception{
Person p=new Person();
Class clazz=Class.forName("com.itcast.refelect.Person");
Field f=clazz.getDeclaredField("password");
f.setAccessible(true);
System.out.println(f.get(p));
}
//private static int age=12;
@Test
public void test3()throws Exception{
Person p=new Person();
Class clazz=Class.forName("com.itcast.refelect.Person");
Field f=clazz.getDeclaredField("age");
f.setAccessible(true);
System.out.println(f.get(p));
}
}
|