简单原理介绍java反射(2)

public class Person {

	private String name;
	private int age;

	public Person(String name,int age) {
		super();
		this.name = name;
		this.age = age;
	}
	
	public   Person(){}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";
	}
	
	public void show()
	{
		System.out.println("show");
	}
	
	public void fun(String str)
	{
		System.out.println(str);
	}
	
	public static void function()
	{
		System.out.println("static");
	}
	

}
public class Demo1 {

	/**
	 * 反射:动态获取字节码文件对象(Person.class),并对其成员进行运行
	 * 
	 * 动态获取字节码文件对象的方式:
	 * 1:任何一个对象都是由字节码文件对象创建的,所以任何一个对象都可以得到自己的字节码文件对象
	 *   那么这个功能应该定义在Object中,所以使用 getClass()
	 *   需要先new对象
	 *   
	 * 2:每种数据类型都有一个 静态的class 属性,通过该属性可以得到字节码文件对象
	 *   不需要new对象,但是需要Person类存在
	 *   
	 * 3:Class类提供了一个静态的forName(String str)方法 
	 *   只需要提供字符串形式的包名+类名
	 * @throws ClassNotFoundException 
	 */
	public static void main(String[] args) throws ClassNotFoundException {
		
          getClaz();
          getClaz2();
          getClaz3();
	}
	//Class类提供了一个静态的forName
	public static void getClaz3() throws ClassNotFoundException
	{
		Class<?> claz1 = Class.forName("com.qianfeng.reflect.Person");
		
		Class<?> claz2 = Class.forName("com.qianfeng.reflect.Person");
		
		System.out.println(claz1==claz2);
	}
	
	
	
	
	//通过静态属性class获取字节码文件对象
	public static void getClaz2()
	{
		Class<Person> claz1 = Person.class;
		
		Class<Person> claz2 = Person.class;
		
		System.out.println(claz1==claz2);
	}
	
	//1:使用Object中的 getClass()获取字节码文件对象
	public static void getClaz()
	{
//		Person person1 = new Person();//先加载Person.class到方法区
//		Class<? extends Person> claz1 = person1.getClass();//得到了 Person.class
//		
//		Person person2 = new Person();
//		Class<? extends Person> claz2 = person2.getClass();//得到了 Person.class
//		
//		System.out.println(claz1==claz2);//true
		
	}

}

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

public class Demo2 {

	/**
	 * 动态获取字节码文件对象,并创建对象
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws ClassNotFoundException 
	 * @throws InvocationTargetException 
	 * @throws IllegalArgumentException 
	 * @throws SecurityException 
	 * @throws NoSuchMethodException 
	 */
	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
		
		createObj1();
		createObj2();
	}
	
	//使用带参数的构造方法创建对象
	public static void createObj2() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
	{   //Person person = new Person("小红",20);
		//获取字节码文件对象---Person.class
		Class<?> claz = Class.forName("com.qianfeng.reflect.Person");
		
		//得到构造方法所属的Constructor类型的对象
		Constructor<?> constructor = claz.getConstructor(String.class,int.class);
		
		//使用构造方法创建对象--使用Constructor提供的创建对象的功能
		Person person =(Person)constructor.newInstance("小红",20);
		
		System.out.println(person);
		
	}
	
	
	
	
	
	//当空参的构造方法不存在时,会发生InstantiationException
	//当构造方法的权限过低时,会发生IllegalAccessException
	public static void createObj1() throws ClassNotFoundException, InstantiationException, IllegalAccessException
	{
		//Person person = new Person();
		//获取字节码文件对象---Person.class
		Class<?> claz = Class.forName("com.qianfeng.reflect.Person");
		
		//使用Class提供的newInstance()方法创建Person类型的对象
		Object obj = claz.newInstance();//使用空参的构造方法创建对象
		
		Person person = (Person)obj;
		
		System.out.println(person);
	}

}







import java.lang.reflect.Field;

public class Demo3 {

	/**
	 * 动态创建对象并给属性赋值
	 * @throws ClassNotFoundException 
	 * @throws SecurityException 
	 * @throws NoSuchFieldException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, SecurityException, InstantiationException, IllegalAccessException {
		
		//Person person = new Person();
		//person.setName("小红");
		
		//获取字节码文件对象---Person.class
	     Class<?> claz = Class.forName("com.qianfeng.reflect.Person");
	     
	     //得到属性name所属的 Field类型的对象
	     //Field field = claz.getField("name");//只能获取权限是public 的属性
	     //System.out.println(field);//NoSuchFieldException
	     
	     Field field = claz.getDeclaredField("name");
	     
	     //因为name是非静态属性,所以必须通过对象访问,所以先创建对象
	     Object obj = claz.newInstance();
	     
	     //设置name属性为可访问的
	     field.setAccessible(true);// 该方法是从父类中继承的
	     
	     //使用Field类提供的赋值功能,给属性赋值
	     field.set(obj, "小红");
	     
	     System.out.println(obj);
	     
	     
		

	}
	
	

}








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

public class Demo4 {

	/**
	 * 动态创建对象,并调用方法
	 * @throws InvocationTargetException 
	 * @throws IllegalArgumentException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws SecurityException 
	 * @throws NoSuchMethodException 
	 * @throws ClassNotFoundException 
	 */
	public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		
		method1();
		method2();
		method3();
	}
	//调用静态方法
	public static void method3() throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
	{
		//获取字节码文件对象---Person.class
	     Class<?> claz = Class.forName("com.qianfeng.reflect.Person");
	     
	     ///获取被调用的方法所属的 Method类型的对象
	     Method method = claz.getMethod("function", null);
	     
	     //执行方法
	     method.invoke(null, null);
	     
	}
	
	//调用带参的构造方法
	public static void method2() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
	{
		//获取字节码文件对象---Person.class
	     Class<?> claz = Class.forName("com.qianfeng.reflect.Person");
	     
	   //获取被调用的方法所属的 Method类型的对象
	     Method method = claz.getMethod("fun", String.class);
	     
	     //fun()属于非静态方式,需要对象去调用
	     Object obj = claz.newInstance();
	     
	     //执行方法
	     method.invoke(obj, "hello");
	}
	
	
	//调用无参的构造方法
	public static void method1() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
	{
		// Person person = new Person();person.show();
		//获取字节码文件对象---Person.class
	     Class<?> claz = Class.forName("com.qianfeng.reflect.Person");
	     
	     //获取被调用的方法所属的 Method类型的对象
	     Method method = claz.getMethod("show", null);
	     
	     
	     //show()属于非静态方式,需要对象去调用
	     Object obj = claz.newInstance();
	     
	     //执行方法
	     method.invoke(obj, null);
	    		 
	     
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值