反射原理及应用(Reflect)


        JAVA反射的定义:JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。


        反射就是java类中的各种成分映射成相应的java类


视频笔记

Class : java程序中的各个java类属于同一类事物,描述这类事物的java类名就是Class。

package com.itheima.shipin.fanshe;

public class reflectTest {

	public static void main(String[] args) throws Exception {
		Class test1 = String.class;
		Class test2 = Class.forName("java.lang.String");
		
		System.out.println(test1);
		System.out.println(test2);
		
		System.out.println(test1 == test2);
	}
}
运行结果:

class java.lang.String

class java.lang.String

true


Method : 方法的类型。

Constructor:代表某个类里的一个构造方法。

package com.itheima.shipin.fanshe;

import java.lang.reflect.Constructor;

public class construtoerTest {

	public static void main(String[] args) throws Exception {

		Constructor constructor = String.class.getConstructor(StringBuffer.class);
		String str = (String) constructor.newInstance(new StringBuffer("heima"));
		System.out.println(str.charAt(4));
	}
}
运行结果

a

Field:代表某个类中的一个成员变量

建立一个sum类代码如下

package com.itheima.shipin.fanshe;

public class sum {
	public int a;
	private int b;

	public sum(int a, int b) {
		super();
		this.a = a;
		this.b = b;
	}
}


建立一个fieldTest.java

package com.itheima.shipin.fanshe;

import java.lang.reflect.Field;

public class fieldTest {

	public static void main(String[] args) throws Exception {
		sum summ = new sum(4, 2);
		Field fielda = summ.getClass().getField("a");
		System.out.println(fielda.get(summ));

		Field fieldb = summ.getClass().getDeclaredField("b");
		fieldb.setAccessible(true);         //暴力读取
		System.out.println(fieldb.get(summ));
	}

}
运行结果:

4
2


应反射将任意一个对象中的所有String类型的成员变量所对应的字符串内容中的“b”改成“a”。

建立一个string类并创建str1,str2,str3三个String类型变量给他们各取值为ball,black,itheima

代码如下

package com.itheima.shipin.fanshe;

public class string {
	public String str1 = "ball";
	public String str2 = "black";
	public String str3 = "itheima";

	public String toString() {
		return str1 + ":" + str2 + ":" + str3;  //建立一各toString()方法查看结果
	}

}

利用反射处理string的代码如下

package com.itheima.shipin.fanshe;

import java.lang.reflect.Field;

public class changeString {

	public static void main(String[] args) throws Exception {
		string str = new string();
		System.out.println(str);		//查看str的初始值
		Field[] fields = str.getClass().getFields();		//取出str中的成员变量
		for (Field field : fields) {
			if (field.getType() == String.class) {		//如果变量为String类型 执行下列,否则跳过
				String oldValue = (String) field.get(str);
				String newValue = oldValue.replace("b", "a"); //将变量中的“b”改成“a”
				field.set(str, newValue);
			}
		}
		System.out.println(str);	//查看str处理后的结果
	}

}

运行结果

ball:black:itheima
aall:alack:itheima


        java框架就是一些类和接口的集合,通过这些类和接口协调来完成一系列的程序实现。JAVA框架可以分为三层:表示层,业务层和物理层。框架又叫做开发中的半成品,它不能提供整个WEB应用程序的所有东西,但是有了框架,我们就可以集中精力进行业务逻辑的开发而不用去关心它的技术实现以及一些辅助的业务逻辑。大家熟知的Structs和Spring就是表示层和业务层框架的强力代表。

       反射其实就是程序能够自检查自身信息。就像程序会照镜子反光看自己。在程序中可以检查某个类中的方法属性等信息,并且能够动态调用。这样可以写出很灵活的程序。比如要把一个对象中的数据copy到另外一个对象中,规则是属性名相同就copy,就可以用反射来做,不需要指定每个属性的名字,只要动态从类中取得信息,再判断属性名是否相同即可。

       用反射模拟框架以类加载器的方式管理资源和配置文件的原理,建立config.properties文件并写入className=java.util.HashSet创建简单的配置信息并读取。

代码如下

package com.itheima.shipin.fanshe;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.InputStream;
import java.util.Collection;
import java.util.Properties;

public class ReflectTest {

	public static void main(String[] args) throws Exception {
		// InputStream ips = new FileInputStream("config.properties");
		InputStream ips = ReflectTest.class
				.getResourceAsStream("config.properties");
		Properties props = new Properties();
		props.load(ips);
		ips.close();
		String className = props.getProperty("className");
		Collection collections = (Collection) Class.forName(className)
				.newInstance();

		System.out.println(collections.getClass().getName());
	}

}

运行结果

java.util.HashSet




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值