Java中的反射总结(实例源码有助于理解)

1.Filed类
package com.my.reflection;

import java.lang.reflect.Field;


/*
 * 总结:通过反射的方式可以获得一个类的成员属性信息(公有的和非公有的)
 * 
 * Field java.lang.Class.getField(String name) throws NoSuchFieldException, SecurityException
 * 此方法只能获取public的属性,否则会抛出异常;参数是对应的属性的名字
 * 
 * Field java.lang.Class.getDeclaredField(String name) throws NoSuchFieldException, SecurityException
 * 此方法可以获取获取所有的属性,如果属性名字写错了,会抛出异常;参数是对应的属性的名字
 * 
 * Field[] java.lang.Class.getFields() throws SecurityException
 * 此方法是获取所有的公有的属性,注意返回值是Field数组
 * 
 * Field[] java.lang.Class.getDeclaredFields() throws SecurityException
 * 此方法是获取所有的属性包括私有的、公有的、受保护的、缺省的
 */
public class Test2 {

	//私有的属性
	private  String  name="LL";
	//共有的属性
	public int age=23;
	//缺省的属性
	String sex="男";
	//受保护的属性
	protected int height=45741;
	//共有的属性
	public int a=451;
	//私有的属性
	private double salary=7890.89;
	
	
	public static void main(String[] args) throws Exception {
		Class c1=Class.forName("com.my.reflection.Test2");
        //通过反射的方式访问共有的属性
		Field filed1=c1.getField("age");//参数是属性的名字
		Field filed2=c1.getField("a");//参数是属性的名字
	    //public int com.my.reflection.Test2.age
		//public int com.my.reflection.Test2.a
		//上面是输出的结果(getFiled方法的)
		Field filed3=c1.getDeclaredField("name");//private java.lang.String com.my.reflection.Test2.name
		Field filed4=c1.getDeclaredField("salary");
		Field filed5=c1.getDeclaredField("sex");
		Field filed6=c1.getDeclaredField("height");
		System.out.println("共有的属性"+filed1);
		System.out.println("共有的属性"+filed2);
		System.out.println("私有的属性"+filed3);
		System.out.println("私有的属性"+filed4);//private double com.my.reflection.Test2.salary
		System.out.println("缺省的属性"+filed5);//java.lang.String com.my.reflection.Test2.sex
		System.out.println("受保护的属性"+filed6);
		System.out.println("------------------------测试Filed的其它方法--------------------------------");
		//获取所有公有即public的属性,注意返回值是Field数组
		Field[] fields1=c1.getFields();
		//循环遍历
		for(Field field:fields1) {
//			输出结果
			//public int com.my.reflection.Test2.age
//			public int com.my.reflection.Test2.a
			System.out.println(field);
		}
		System.out.println("-------------------华丽的分割线---------------------");
		//获取所有的属性(公有的、私有的、受保护的、缺省的)
		Field[] fields2=c1.getDeclaredFields();
		//循环遍历
				for(Field field:fields2) {
//					输出结果
					System.out.println(field);
				}
	}

}

运行结果
在这里插入图片描述

2.Constructor类

package com.my.reflection;

import java.lang.reflect.Constructor;

public class Test1 {
	public Test1() {
		System.out.println("Test1的无参构造函数被调用了");
	}
	public Test1(int x) {
		System.out.println("Test1的一个参数的构造函数被调用了,x的值:"+x);
	}
	public Test1(int x,String name) {
		System.out.println("Test1的两个参数的构造函数被调用了,x的值,name的值:"+x+" "+name);
	}
	public Test1(int x,String name,double salary) {
		System.out.println("Test1的三个参数的构造函数被调用了,x的值,name的值,salary的值:"+x+" "+name+" "+salary);
	}

    public static void main(String[] args) throws Exception {
		//获取字节码对象的一种新的方式,这里有问题,所以不行
//    	Class c1=ClassLoader.LoadClass("com.my.Test1");
    	try {
			Class c1=Class.forName("com.my.reflection.Test1");
			//创建对象
//			Object obj1=c1.newInstance();
			c1.newInstance();//只要调用了newINstance方法就会调用无参构造方法
			
			Constructor con1=c1.getConstructor(int.class);//这里传入构造函数的参数的字节码
			con1.newInstance(15);//这里传入构造函数的参数值
			
			//调用两个参数的构造函数,注意参数的顺序不能写错了,否则会报错的
//			Constructor con2=c1.getConstructor(String.class,int.class);
			Constructor con2=c1.getConstructor(int.class,String.class);
			con2.newInstance(23,"LL");//newInstance方法的参数就是对应的类的构造函数要传入的值
			
			//三个参数的构造函数,不能写成下面这样的
//			Constructor con3=c1.getConstructor(int.class,String.class,Double.class);
			Constructor con3=c1.getConstructor(int.class,String.class,double.class);
			con3.newInstance(23,"LL",12000.45445);
    	} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
			
			//这个异常是检查型(也就是编译时,就是编译器检查时)异常ClassNotFoundException
			e.printStackTrace();
		}
	}
}


运行结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值