反射
一, 反射就是加载类,并解剖出类的各个组成部分。一个类有多个组成部分,如:成员变量(filed),方法(method),构造方法(constructor)组成。
二, 反射分为两个步骤,(一)加载类,(二),解剖出类的各个组成部分。
(一)加载类,获得类的字节码。
1,通过Class的forName方法获得类的字节码
Class clazz = Class.forName(“cn.cyy.reflect.Person”);
2.通过new对象,再调用对象的getClass方法得到字节码
Class clazz1 = new Person().getClass();
3.直接把类的字节码赋值给Class
Class clazz2 = Person.class;
package cn.cyy.reflect;
public class Demo1 {
/**
* 反射:加载类,获得类的字节码
* @param args
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws ClassNotFoundException {
//1,通过Class的forName方法获得类的字节码
Class clazz = Class.forName("cn.cyy.reflect.Person");
//2.通过new对象,再调用对象的getClass方法得到字节码
Class clazz1 = new Person().getClass();
//3.直接把类的字节码赋值给Class
Class clazz2 = Person.class;
}
}
二,反射出类的构造函数,字段,方法
1,反射出类构造方法
package cn.cyy.reflect;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
/**
* 反射类的构造函数,创建对象
* @author Administrator
*
*/
public class Demo2 {
//反射构造函数:public Person()
@Test
public void test1() throws Exception{
Class clazz = Class.forName("cn.cyy.reflect.Person");
Constructor c = clazz.getConstructor(null);
Object o = c.newInstance(null);
Method m = clazz.getMethod("getName",null);
System.out.println(m.invoke(o, null));
}
//public Person(String name)
@Test
public void test2() throws Exception{
Class clazz = Class.forName("cn.cyy.reflect.Person");
Constructor c = clazz.getConstructor(String.class);
Object obj = c.newInstance("yuli@cyy");
Method m = clazz.getMethod("getName",null);
System.out.println(m.invoke(obj, null));
}
//反射:public Person(String name,int password)
@Test
public void test3() throws Exception{
Class clazz = Class.forName("cn.cyy.reflect.Person");
Constructor c = clazz.getConstructor(String.class,int.class);
Object obj = c.newInstance("yuli",18);
Method m = clazz.getMethod("getName",null);
System.out.println(m.invoke(obj, null));
}
//反射:private Person(List list)
@Test
public void test4() throws Exception{
Class clazz = Class.forName("cn.cyy.reflect.Person");
Constructor c = clazz.getDeclaredConstructor(List.class);
c.setAccessible(true);
Object obj = c.newInstance(new ArrayList());
Method m = clazz.getMethod("getName",null);
System.out.println(m.invoke(obj, null));
}
}
person类
package cn.cyy.reflect;
import java.io.InputStream;
import java.util.List;
public class Person {
private String name = "cyy";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Person(){
System.out.println("person");
}
public Person(String name){
System.out.println("person "+name);
}
public Person(String name,int password){
System.out.println(name+":"+password);
}
private Person(List list){
System.out.println("list"+list);
}
public void testRft(){
System.out.println("testRft");
}
public void testRft(String name,int password){
System.out.println(name+":"+password);
}
public Class[] testRft(String name,int[] password){
return new Class[]{String.class};
}
private void testRft(InputStream in){
System.out.println(in);
}
public static void testRft(int num){
System.out.println(num);
}
public static void main(String []args){
System.out.println("main");
for (String str : args) {
System.out.println(str);
}
}
}
2,反射出类的方法
package cn.cyy.reflect;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Method;
import org.junit.Test;
//反射类的方法
public class Demo3 {
//反射类的方法:public void testRft()
@Test
public void test1() throws Exception{
Class clazz = Class.forName("cn.cyy.reflect.Person");
Object obj = clazz.newInstance();
Method method = clazz.getMethod("testRft", null);
method.invoke(obj, null);
}
//反射类的方法:public void testRft(String name,int password)
@Test
public void test2() throws Exception{
Class clazz = Class.forName("cn.cyy.reflect.Person");
Object obj = clazz.newInstance();
Method method = clazz.getMethod("testRft", String.class,int.class);
method.invoke(obj, "cyy",24);
}
//反射类带有复杂参数的方法:public Class[] testRft(String name,int[] password)
@Test
public void test3() throws Exception{
Class clazz = Class.forName("cn.cyy.reflect.Person");
Object obj = clazz.newInstance();
Method method = clazz.getMethod("testRft", String.class,int[].class);
Class[] cl = (Class[]) method.invoke(obj, "cyy&yuli",new int[]{1,2,3});
System.out.println(cl[0]);
}
//反射类的私有方法:private void testRft(InputStream in)
@Test
public void test4() throws Exception{
Class clazz = Class.forName("cn.cyy.reflect.Person");
Object obj = clazz.newInstance();
Method method = clazz.getDeclaredMethod("testRft", InputStream.class);
method.setAccessible(true);
method.invoke(obj, new FileInputStream("c:\\log_network.txt"));
}
//反射类的静态方法:public static void testRft(int num)
@Test
public void test5() throws Exception{
Class clazz = Class.forName("cn.cyy.reflect.Person");
Method method = clazz.getMethod("testRft", int.class);
method.invoke(null, 23);
}
//反射类的main方法:public static void main(String []args)
@Test
public void test6() throws Exception{
Class clazz = Class.forName("cn.cyy.reflect.Person");
Method method = clazz.getMethod("main", String[].class);
method.invoke(null, new Object[]{new String[]{"1","23","456"}});
}
}
3,反射类的字段
package cn.cyy.reflect;
import java.lang.reflect.Field;
import org.junit.Test;
public class Demo4 {
//反射私有字段:private String name = "cyy";
@Test
public void test1() throws Exception{
Class clazz = Class.forName("cn.cyy.reflect.Person");
Object obj = clazz.newInstance();//加载类创建对象
Field f = clazz.getDeclaredField("name");//得到name字段
Class type = f.getType();//得到name字段的类型
f.setAccessible(true);//打开私有字段的访问权限
if(type.equals(String.class)){//如果字段是字符串,则赋值
f.set(obj, "yuli");//为字段name赋值
System.out.println(f.get(obj));//取出字段name的值
}
}
//反射公有字段:public int password = 123;
@Test
public void test2() throws Exception{
Class clazz = Class.forName("cn.cyy.reflect.Person");
Object obj = clazz.newInstance();//加载类创建对象
Field f = clazz.getField("password");
Class type = f.getType();
if(type.equals(int.class)){
f.set(obj, 567);
System.out.println(f.get(obj));
}
}
//反射类的静态字段:public static int age = 23;
@Test
public void test3() throws Exception{
Class clazz = Class.forName("cn.cyy.reflect.Person");
Object obj = clazz.newInstance();//加载类创建对象
Field f = clazz.getField("age");
Class type = f.getType();
if(type.equals(int.class)){
f.set(obj, 18);
System.out.println(f.get(obj));
}
}
}