Java反射

一、Java反射的基础——Class类

Instances of the class Class represent classes and interfaces in a running Java application.
Class类的实例表示运行中的Java程序的类和接口。

意思是每个载入内存中的类,都有一个对应的Class类,而且该Class类是唯一的。
例如,
String str1 = new String("abc");
String str2 = new String("abc");
String str3 = new String("abcdefg");
System.out.println(str1.getClass() == str2.getClass()); // true
System.out.println(str1.getClass() == str3.getClass()); // true

获取Class类的三种方法
1.类名.class
例如,
Class<String> clazz = String.class;

2.对象.getClass()
例如,
Class<? extends String> x = str.getClass();

3.Class.forName("类名")
try {
	Class.forName("java.util.Date");
} catch (ClassNotFoundException e) {
	e.printStackTrace();
}

二、反射的应用
通过反射实例化类,即使该类是private也无所谓
通过反射获取指定字段的值
通过反射获取获取指定方法,然后调用该方法
示例:

public class Bean {
	private int id;
	private String name;
	public String address;
	public static int count;

	private Bean() {
		id = 100;
		name = "BB";
		address = "天堂路1号";
	}

	public Bean(int id, String name, String address) {
		this.id = id;
		this.name = name;
		this.address = address;
	}

	private void printInfo() {
		System.out.println("id = " + id + ", name = " + name + ", address = " + address);
	}

	public static void getCount() {
		System.out.println("count = " + count);
	}
}


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

public class ReflectDemo {
	public static void main(String[] args) {
		Class clazz = null;
		try {
			clazz = Class.forName("Bean");
			Constructor constructor = clazz.getDeclaredConstructor(null);
			System.out.println("默认构造方法:" + constructor.toString());

			// java.lang.IllegalAccessException
			constructor.setAccessible(true);

			Object obj = constructor.newInstance(null);

			Field address = clazz.getField("address");
			Object addressValue = address.get(obj);
			System.out.println("address = " + addressValue);

			// java.lang.NoSuchFieldException: id
			// 因为id是private类型,应该使用getDeclaredField()方法
			// Field id = clazz.getField("id");
			Field id = clazz.getDeclaredField("id");

			// java.lang.IllegalAccessException
			id.setAccessible(true);

			Object idValue = id.get(obj);
			System.out.println("id = " + idValue);

			Method privateMethod = clazz.getDeclaredMethod("printInfo", null);
			privateMethod.setAccessible(true);
			privateMethod.invoke(obj, null);

			Method staticMethod = clazz.getMethod("getCount", null);
			staticMethod.invoke(null);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
/*
输出内容:

默认构造方法:private Bean()
address = 天堂路1号
id = 100
id = 100, name = BB, address = 天堂路1号
count = 0

*/


三、其他信息
	public static void other(){
		int[] a1 = new int[3];
		int[] a2 = new int[4];
		int[][] a3 = new int[2][3];
		String[] a4 = new String[3];
		
		System.out.println(a1.getClass().getName());
		System.out.println(a2.getClass().getName());
		System.out.println(a3.getClass().getName());
		System.out.println(a4.getClass().getName());
		
		System.out.println(a1.getClass().getSuperclass().getName());
		System.out.println(a4.getClass().getSuperclass().getName());
		
		System.out.println(a1.getClass() == a2.getClass());
		// Incompatible operand types
//		System.out.println(a1.getClass() == a3.getClass());
//		System.out.println(a1.getClass() == a4.getClass());
		
		Object obj1 = a1;
		Object obj2 = a4;
//		Object[] obj3 = a1;  // cannot convert from int[] to Object[]
		Object[] obj4 = a3;
		Object[] obj5 = a4;
		
/*
输出内容:

[I
[I
[[I
[Ljava.lang.String;
java.lang.Object
java.lang.Object
true
*/
	}
	
	public static void printObject(Object obj){
		
		Class clazz = obj.getClass();
		if(clazz.isArray()){
			for(int i=0;i<Array.getLength(obj);++i){
				System.out.println(Array.get(obj, i));
			}
		}else{
			System.out.println(obj);
		}
		
	}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值