反射

1.反射 : 加载类并剖解类的各个组成部分(1.构造函数 2.方法  3.字段)

待剖解类Person.java

import java.io.InputStream;
import java.util.List;

public class Person {

	public String name = "haha";
	private int password = 123;
	private static int age = 23;

	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 list) {
		System.out.println("list");
	}

	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("main !!!");
	}
}
反射构造函数,目的创建类对象

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

//解剖构造函数 ,创建类的对象
public class Demo2 {

	// 反构造函数 public Person()
	@Test
	public void test1() throws Exception {
		Class clazz = Class.forName("com.reflect.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("com.reflect.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("com.reflect.Person");
		Constructor c = clazz.getConstructor(String.class, int.class);
		Person p = (Person) c.newInstance("xxx", 12);
		System.out.println(p.name);

	}

	// 反构造函数 private Person(List list)
	@Test
	public void test4() throws Exception {
		Class clazz = Class.forName("com.reflect.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("com.reflect.Person");
		Person p = (Person) clazz.newInstance();
		System.out.println(p.name);
	}
}

反射方法,目的创建类方法

import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Method;

import org.junit.Test;

//反射类的方法
public class Demo3 {

	// public void aa1()
	@Test
	public void test1() throws Exception {

		Person p = new Person();

		Class clazz = Class.forName("com.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.reflect.Person");
		Method method = clazz.getMethod("aa1", String.class, int.class);
		method.invoke(p, "xxx", 38);
	}

	// public void aa1(String name,int password)
	@Test
	public void test3() throws Exception {

		Person p = new Person();

		Class clazz = Class.forName("com.reflect.Person");
		Method method = clazz.getMethod("aa1", String.class, int.class);
		method.invoke(p, "xxx", 38);
	}

	// public Class[] aa1(String name,int[] password)
	@Test
	public void test4() throws Exception {

		Person p = new Person();

		Class clazz = Class.forName("com.reflect.Person");
		Method method = clazz.getMethod("aa1", String.class, int[].class);
		Class[] cs = (Class[]) method.invoke(p, "aaa", new int[] { 1, 2, 3 });
		System.out.println(cs[0]);
	}

	// private void aa1(InputStream in)
	@Test
	public void test5() throws Exception {

		Person p = new Person();

		Class clazz = Class.forName("com.reflect.Person");
		Method method = clazz.getDeclaredMethod("aa1", InputStream.class);
		method.setAccessible(true);
		method.invoke(p, new FileInputStream("C:\\1.txt"));

	}

	// public static void aa1(int num)
	@Test
	public void test6() throws Exception {

		Person p = new Person();
		Class clazz = Class.forName("com.reflect.Person");

		Method method = clazz.getMethod("aa1", int.class);
		method.invoke(null, 23);
	}

	//public static void main(String[] args)
	@Test
	public void test7() throws Exception {

		Class clazz = Class.forName("com.reflect.Person");

		Method method = clazz.getMethod("main",String[].class);
//		method.invoke(null,new Object[]{new String[]{"aa","123"}});
		method.invoke(null, (Object)new String[]{"aa","123"});
	}
}
       反射字段,目的封装对象
import java.lang.reflect.Field;

import org.junit.Test;

//反射字段
public class Demo4 {

	// 反射字段:public String name = "haha";
	@Test
	public void test1() throws Exception {

		Person p = new Person();

		Class clazz = Class.forName("com.reflect.Person");
		Field f = clazz.getField("name");

		Object obj = f.get(p); // 获取字段值
		Class type = f.getType(); // 获取字段类型

		if (type.equals(String.class)) {
			String value = (String) obj;
			System.out.println(value);
		}

		// 设置字段的值
		f.set(p, "XXXXXXXX");
		System.out.println(p.name);
	}

	// 反射字段:private int password;
	@Test
	public void test2() throws Exception {

		Person p = new Person();

		Class clazz = Class.forName("com.reflect.Person");
		Field f = clazz.getDeclaredField("password");
		f.setAccessible(true);

		System.out.println(f.get(p));
	}

	// 反射字段:private static int age = 23;;
	@Test
	public void test3() throws Exception {

		Person p = new Person();

		Class clazz = Class.forName("com.reflect.Person");
		Field f = clazz.getDeclaredField("password");
		f.setAccessible(true);

		System.out.println(f.get(p));
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值