java反射的基本使用

package cn.itcast;

public class Person {
	public String name="aaa";
	private String password="hhh";
	public static String repassword="dadwad";
	public Person()
	{
		System.out.println("person");
	}
	public Person(String name)
	{
		this.name=name;
		System.out.println(this.name);
	}
	public Person(String name,int[] score)
	{
		this.name=name;
		System.out.println(this.name);
		for(Object obj:score)
		{
			System.out.println(obj);
		}
	}
	private Person(String password,int id)
	{
		this.password=password;
		System.out.println(this.password+" "+id);
		
	}
	public void add()
	{
		System.out.println("person.add()");
	}
	public void add(String name)
	{
		this.name=name;
		System.out.println(name);
	}
	private int add(int a,int b)
	{
		return a+b;
	}
	public static void mian(String[] arg0) {
		// TODO Auto-generated method stub
		System.out.print("main()");

	}

}

package cn.itcast;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		//test1();
		//test2();
		//text3();
		//text4();
		
		
		//text5();
		//text6();
		//text7();
		//text8();
		//text9();
		text10();
		
	
		
		
	}
	//反射私有字段
	private static void text10() throws ClassNotFoundException,
			NoSuchFieldException, IllegalAccessException {
		Person p=new Person();
		Class clazz=Class.forName("cn.itcast.Person");
		Field name = clazz.getField("name");
		name.setAccessible(true);
		System.out.println(name.get(p));
		name.set(p,"hhah");
		System.out.println(name.get(p));
	}
	//反射公有字段
	private static void text9() throws ClassNotFoundException,
			NoSuchFieldException, IllegalAccessException {
		Person p=new Person();
		Class clazz=Class.forName("cn.itcast.Person");
		Field name = clazz.getField("name");
		System.out.print(name.get(p));
	}
	
	//反射主函数
	private static void text8() throws ClassNotFoundException,
			NoSuchMethodException, IllegalAccessException,
			InvocationTargetException {
		Person p=new Person();
		Class clazz=Class.forName("cn.itcast.Person");
		Method men = clazz.getDeclaredMethod("mian",String[].class);
		men.setAccessible(true);
		System.out.print(men.invoke(p,(Object)(new String[]{"123","awdw"})));
	}
	//反射私有有参函数
	private static void text7() throws ClassNotFoundException,
			NoSuchMethodException, IllegalAccessException,
			InvocationTargetException {
		Person p=new Person();
		Class clazz=Class.forName("cn.itcast.Person");
		Method men = clazz.getDeclaredMethod("add",int.class,int.class);
		men.setAccessible(true);
		System.out.print(men.invoke(p, 1,2));
	}
	
	//反射有参函数
	private static void text6() throws ClassNotFoundException,
			NoSuchMethodException, IllegalAccessException,
			InvocationTargetException {
		Person p=new Person();
		Class clazz=Class.forName("cn.itcast.Person");
		Method men = clazz.getMethod("add",String.class);
		men.invoke(p,"zhangsan");
	}
	
	//反射无参函数
	private static void text5() throws ClassNotFoundException,
			NoSuchMethodException, IllegalAccessException,
			InvocationTargetException {
		Person p=new Person();
		Class clazz=Class.forName("cn.itcast.Person");
		Method men = clazz.getMethod("add", null);
		men.invoke(p, null);
	}

	//反射私有多参构造函数
	private static void text4() throws ClassNotFoundException,
			NoSuchMethodException, InstantiationException,
			IllegalAccessException, InvocationTargetException {
		Class clazz=Class.forName("cn.itcast.Person");
		Constructor con = clazz.getDeclaredConstructor(String.class,int.class);
		con.setAccessible(true);//强暴私有成员
		Person p=(Person) con.newInstance("enen",12);
	}
	
	//反射多参构造方法
	private static void text3() throws ClassNotFoundException,
			NoSuchMethodException, InstantiationException,
			IllegalAccessException, InvocationTargetException {
		Class clazz=Class.forName("cn.itcast.Person");
		Constructor con = clazz.getConstructor(String.class,int[].class);
		Person p=(Person) con.newInstance("dawdwad",new int[]{1,2,3,5});
	}
	//反射有参构造方法
	private static void test2() throws ClassNotFoundException,
			NoSuchMethodException, InstantiationException,
			IllegalAccessException, InvocationTargetException {
		Class clazz=Class.forName("cn.itcast.Person");
		Constructor con = clazz.getConstructor(String.class);
		Person p=(Person) con.newInstance("zhangsan");
	}
//反射无参构造方法
	private static void test1() throws ClassNotFoundException,
			NoSuchMethodException, InstantiationException,
			IllegalAccessException, InvocationTargetException {
		Class clazz=Class.forName("cn.itcast.Person");
		Constructor can = clazz.getConstructor(null);
		Person p=(Person) can.newInstance(null);
		
	}
	


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值