Java的反射机制和其简单的应用

什么是反射

反射定义十分的难以理解但是我们可以知道反射可以做些什么?
Java的反射机制是在运行转态,对于任意个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用他的任意个方法和属性,这种动态获取信息以及动态调用对象方法的功能称为Java的反射机制。

Class类

要创建Class类的对象有3种方法
(1)使用class属性

Class classDemo = Demo.class;

(2)使用Class类的forName方法

Class classDemo = Class.forName("test.Demp");

(3)使用对象的getClassff

Demo demo = new Demo();
Class classDemo = demo.getClass();

用Class类来实现构造方法

(1)先创建一个Dog类

public class Dog {
	private String name;
	private String sex;
	private String strain;
	private int age;
	private double health;
	static{
		System.out.println("----静态方法-----");
	}
	public Dog() {
		super();
		System.out.println("---无参构造--");
	}
	public Dog(String name, String sex, String strain, int age, double health) {
		super();
		this.name = name;
		this.sex = sex;
		this.strain = strain;
		this.age = age;
		this.health = health;
		System.out.println("---有参构造----");
	}
	@Override
	public String toString() {
		return "Dog [age=" + age + ", health=" + health + ", name=" + name
				+ ", sex=" + sex + ", strain=" + strain + "]";
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getStrain() {
		return strain;
	}
	public void setStrain(String strain) {
		this.strain = strain;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public double getHealth() {
		return health;
	}
	public void setHealth(double health) {
		this.health = health;
	}
}

(2)先创建一个无参构造

public static void main(String[] args) throws Exception {
		Class dogClass = Class.forName("com.wbzkc.practise1.Dog");
		Dog dog = (Dog) dogClass.newInstance();
		System.out.println(dog);
	}

运行结果:

----静态方法-----
---无参构造--
Dog [age=0, health=0.0, name=null, sex=null, strain=null]

先通过Class类得到Dog类的class对象,通过class对象的newInstance()方法来创建一个无参构造的对象 ==== Dog dog = new Dog()

(3)实现有参构造的创建

public static void main(String[] args) throws Exception {
		Class dogClass = Class.forName("com.wbzkc.practise1.Dog");
		Constructor constructor = dogClass.getConstructor(String.class,String.class,String.class,int.class,double.class);
		Dog dog = (Dog)constructor.newInstance("张三","公狗","哈士奇",11,9);
		System.out.println(dog);
	}

先通过Class类得到Dog类的class对象,在通过访问构造器Constructor类中的getConstructor(Class…)来获取构造方法里面的属性,最后通过newInstance()来赋值 ==== Dog dog = new Dog(“张三”,“公狗”,“哈士奇”,11,9);

Field方法获得成员属性

(1)方法
getFields() :返回一个Field对象数组,这些对象反应此Class对象所表示的类或者接口的所有可访问的公共字段
getField(String name): 返回一个Field对象,这个对象反应此Class所表示的类和接口的指定的公共成员字段
getDeclaredFields():返回一个Field对象数组,这些对象反应此Class对象所表示的类或者接口的所有可访问的字段
getDeclaredField(String name):返回一个Field对象,这个对象反应此Class所表示的类和接口的指定的成员字段(包括私有)

(2)获得访问修饰符
getModifiers() 返回该访问修饰符对应的整数。
0:默认修饰符
1:public
2:private
4:protected
(3)获得属性类型
getType()
(4)获得属性名称
getName()
(5)代码

public static void main(String[] args) throws Exception {
		Class dogClass = Class.forName("com.wbzkc.practise1.Dog");
		Field[] fields = dogClass.getDeclaredFields();
		for(Field field : fields){
			String fieldName = field.getName();//属性名
			String fieldType = field.getType().getName();//属性类型
			int fieldModifiers = field.getModifiers();//属性访问修饰符
			System.out.println("访问修饰符:" + fieldModifiers + ", 属性类型:" + fieldType + ", 属性名称:" + fieldName);
		}
	}

(6)运行结果

----静态方法-----
访问修饰符:2, 属性类型:java.lang.String, 属性名称:name
访问修饰符:2, 属性类型:java.lang.String, 属性名称:sex
访问修饰符:2, 属性类型:java.lang.String, 属性名称:strain
访问修饰符:2, 属性类型:int, 属性名称:age
访问修饰符:2, 属性类型:double, 属性名称:health

(7)在Field中也是可以给公共的属性赋值,但是不推荐,因为基本上物品类的属性都是私有化的
先把Dog类中的name属性private改为public方便以下操作,结束后要改回来
公共属性:

public static void main(String[] args) throws Exception {
		Class dogClass = Class.forName("com.wbzkc.practise1.Dog");
		Dog dog = (Dog) dogClass.newInstance();
		Field field = dogClass.getField("name");
		field.set(dog, "张三");
		System.out.println(dog);
}
----静态方法-----
---无参构造--
Dog [age=0, health=0.0, name=张三, sex=null, strain=null]

私有属性

Class dogClass = Class.forName("com.wbzkc.practise1.Dog");
		Dog dog = (Dog) dogClass.newInstance();
		Field field = dogClass.getField("strain");
		field.set(dog, "哈士奇");
		System.out.println(dog);
----静态方法-----
---无参构造--
Exception in thread "main" java.lang.NoSuchFieldException: strain
	at java.lang.Class.getField(Class.java:1520)
	at com.wbzkc.practise1.TestDog3.main(TestDog3.java:11)

Method类获得方法

(1)方法:
getMethods():返回一个Method数组,全是公共方法
getMethod(String name ,属性类型.class):返回一个Method对象,指定的公共方法
getDeclaredMethods():返回一个Method数组,全部方法,包含私有方法
getDeclaredMethod(String name,属性类型.class):返回一个Method对象,指定的全部类型的方法
(2)单个赋值代码

public static void main(String[] args) throws Exception {
		Class dogClass = Class.forName("com.wbzkc.practise1.Dog");
		Dog dog = (Dog) dogClass.newInstance();
		Method method = dogClass.getMethod("setName", String.class);
		method.invoke(dog, "张三");
		System.out.println(dog);
	}

结果:

----静态方法-----
---无参构造--
Dog [age=0, health=0.0, name=张三, sex=null, strain=null]

(3)给全部属性赋值

public static void main(String[] args) throws Exception {
		Class dogClass = Class.forName("com.wbzkc.practise1.Dog");
		Dog dog = (Dog) dogClass.newInstance();
		Method[] methods = dogClass.getMethods();
		for(Method method : methods){
			if(method.getName().equals("setName")){
				method.invoke(dog, "张三");
			}else if(method.getName().equals("setSex")){
				method.invoke(dog, "公狗");
			}else if(method.getName().equals("setStrain")){
				method.invoke(dog, "哈士奇");
			}else if(method.getName().equals("setAge")){
				method.invoke(dog, 13);
			}else if(method.getName().equals("setHealth")){
				method.invoke(dog, 9);
			}
		}
		System.out.println(dog);
}

结果:

----静态方法-----
---无参构造--
Dog [age=13, health=9.0, name=张三, sex=公狗, strain=哈士奇]

优化过后的代码:

public static void main(String[] args) throws Exception {
		Class dogClass = Class.forName("com.wbzkc.practise1.Dog");
		Dog dog = (Dog) dogClass.newInstance();
		Field[] fields = dogClass.getDeclaredFields();
		for(Field field : fields){
			String fieldName = field.getName();
			Class methodType = field.getType();
			String methodName = "set" + fieldName.substring(0,1).toUpperCase() + fieldName.substring(1);
			Method method = dogClass.getMethod(methodName, methodType);
			if("int".equals(methodType.getName())){
				method.invoke(dog, 12);
			}else if("double".equals(methodType.getName())){
				method.invoke(dog, 8);
			}else{
				if(methodName.equals("setName")){
					method.invoke(dog, "李四");
				}else if(methodName.equals("setStrain")){
					method.invoke(dog, "哈士奇");
				}else if(methodName.equals("setSex")){
					method.invoke(dog, "母狗");
				}
			}
		}
		System.out.println(dog);
	}
----静态方法-----
---无参构造--
Dog [age=12, health=8.0, name=李四, sex=母狗, strain=哈士奇]

大家发现了没有这种方法虽然可以赋值,但是需要不同的属性,相同的属性还是需要里面在if循环
那么应该怎么办呢?所以需要我们学习Properties文件来解决这一问题。。。

反射机制的应用Properties配置文件

(1)如何解析配置文件

Properties prop = new Propeties();
prop.load(本类.class.getClassLoader().getResourceAsStream("dog.properties"));

或者使用字节流或者字符流

Properties prop = new Properties();
BufferedReader br = null;
br = new BufferedReader(new FileReader(new File("src/dog.properties")));
prop.load(br);

(2)完善上述案例
先写一个dog.properties配置文件:src/dog.properties 文件路径
上述迭代器有很多种方法
(1)Set set = prop.keySet();
BufferedReader br = null;
br = new BufferedReader(new FileReader(new File(“src/dog.properties”)));
prop.load(br);s

public static void main(String[] args) throws Exception {
		Class dogClass = Class.forName("com.wbzkc.practise1.Dog");
		Dog dog = (Dog) dogClass.newInstance();
		Properties prop = new Properties();
		BufferedReader br = null;
		br = new BufferedReader(new FileReader(new File("src/dog.properties")));
		prop.load(br);
		Set<Object> set = prop.keySet();
		Iterator<Object> it = set.iterator();
		while(it.hasNext()){
			String key = (String) it.next();
			String value = prop.getProperty(key);
			//方法名
			String methodName = "set" + key.substring(0,1).toUpperCase() + key.substring(1);
			//根据建值来获取方法名和属性类型
			Field field = dogClass.getDeclaredField(key);
			String fieldName = field.getName();
			Class fieldType = field.getType();
			//根据方法名来获取方法
			Method method = dogClass.getMethod(methodName, fieldType);
			if("int".equals(fieldType.getName())){
				method.invoke(dog, Integer.valueOf(value));
			}else if("double".equals(fieldType.getName())){
				method.invoke(dog, Double.valueOf(value));
			}else{
				method.invoke(dog, value);
			}
		}
		System.out.println(dog);
	}
----静态方法-----
---无参构造--
Dog [age=15, health=23.4, name=zhansgan, sex=dogwoman, strain=hashiqi]

(2)Set<Entry<Object, Object>> set = prop.entrySet();
prop.load(TestDog7.class.getClassLoader().getResourceAsStream(“dog.properties”));

public static void main(String[] args) throws Exception {
		Class dogClass = Class.forName("com.wbzkc.practise1.Dog");
		Dog dog = (Dog) dogClass.newInstance();
		Properties prop = new Properties();
		prop.load(TestDog7.class.getClassLoader().getResourceAsStream("dog.properties"));
		Set<Entry<Object, Object>> set = prop.entrySet();
		Iterator<Entry<Object, Object>> it = set.iterator();
		while(it.hasNext()){
			Entry<Object, Object> entry = it.next();
			String key = (String) entry.getKey();
			String value = (String) entry.getValue();
			String methodName = "set" + key.substring(0,1).toUpperCase() + key.substring(1);
			//根据建值来获取方法名和属性类型
			Field field = dogClass.getDeclaredField(key);
			String fieldName = field.getName();
			Class fieldType = field.getType();
			//根据方法名来获取方法
			Method method = dogClass.getMethod(methodName, fieldType);
			if("int".equals(fieldType.getName())){
				method.invoke(dog, Integer.valueOf(value));
			}else if("double".equals(fieldType.getName())){
				method.invoke(dog, Double.valueOf(value));
			}else{
				method.invoke(dog, value);
			}
		}
}

(3)Enumeration enume = (Enumeration) prop.propertyNames();
省略
(4)ResourceBundle bundle = ResourceBundle.getBundle(“dog”);

public static void main(String[] args) throws Exception {
		Class dogClass = Class.forName("com.wbzkc.practise1.Dog");
		Dog dog = (Dog) dogClass.newInstance();
		ResourceBundle bundle = ResourceBundle.getBundle("dog");
		Field[] fields = dogClass.getDeclaredFields();
		for(Field field : fields){
			String fieldName = field.getName();
			String value = bundle.getString(fieldName);
			Class fieldType = field.getType();
			String methodName = "set" + fieldName.substring(0,1).toUpperCase() + fieldName.substring(1);
			Method method = dogClass.getMethod(methodName, fieldType);
			if("int".equals(fieldType.getName())){
				method.invoke(dog, Integer.valueOf(value));
			}else if("double".equals(fieldType.getName())){
				method.invoke(dog, Double.valueOf(value));
			}else{
				method.invoke(dog, value);
			}
		}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值