黑马程序员——反射

------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------


反射

(1)反射:在程序运行时期,是通过class文件对象,去使用构造方法,成员变量,成员方法。

(2)获取class文件对象

    1:Object类的getClass()方法

    2:数据类型的静态的class属性

    3:ClassforName()静态方法

 

    推荐:开发使用第三种。

 

    整理反射应用的步骤并且写出代码


(3)反射的应用

    1:通过反射获取构造方法并使用

    //获取字节码文件对象

    Class c = Class.forName("cn.itcast.Person");

 

    //获取构造器对象

    Constructor con = c.getDeclearedConstrator(String.class,int.class);

 

    //通过构造器对象获取Person对象

    Object obj = con.newInstance("张三",29);

 

    2:通过反射获取成员变量并使用

    //获取字节码文件对象

    Class c = Class.forName("cn.itcast.Person");

 

    //获取构造器对象

    Constructor con = c.getDeclearedConstrator(String.class,int.class);

 

    //通过构造器对象获取Person对象

    Object obj = con.newInstance("张三",29);

 

 

    //获取成员变量

    Field field = c.getDeclaredField("name");

    field.setAccessiable(true);

    field.set(obj,"李四");

 

 

 

3:通过反射获取成员方法并使用

//获取字节码文件对象

Class c = Class.forName("cn.itcast.Person");

 

//获取构造器对象

Constructor con = c.getDeclearedConstrator(String.class,int.class);

 

//通过构造器对象获取Person对象

Object obj = con.newInstance("张三",29);

 

//获取所有公共方法对象,包括从父类继承的

//(获取本类所有方法的时候,不包括从父类继承的)

Method[] methods = c.getDeclaredMethods();

for(Method m : methods){

m.setAccessiable(true);

m.invoke(obj, null);

}

 

(4)案例

  1、

public class Test {
	public static void main(String[] args) throws Exception {
		// Student s = new Student();
		// s.love();
		// Teacher t = new Teacher();
		// t.love();

		// 如果有需求上的改动,这里的代码一直在发生改动。
		// 通过反射如何来实现呢
		// 反射+配置文件
		//作为配置文件来说
		/*
		 * 键是固定的,是已经知道的。
		 * 值是变化的。
		 * 
		 * className,methodName
		 */
		Properties prop = new Properties();
		FileReader fr = new FileReader("test.properties");
		prop.load(fr);
		fr.close();
		
		//获取类名
		String className = prop.getProperty("className");
		//获取方法名
		String methodName = prop.getProperty("methodName");
		
		//获取字节码文件对象
		Class c = Class.forName(className);
	
		Constructor con = c.getConstructor();
		Object obj = con.newInstance();
		
		Method m = c.getMethod(methodName, null);
		m.invoke(obj, null);
	}
}

2、在这个集合中添加一个字符串数据

import java.lang.reflect.Method;
import java.util.ArrayList;

/*
 * 我给你ArrayList<Integer>的一个对象,我想在这个集合中添加一个字符串数据,如何实现呢?
 * 通过反射实现。
 * 
 * 反射可以越过泛型检查。
 */
public class ArrayListTest {
	public static void main(String[] args) throws Exception {
		ArrayList<Integer> array = new ArrayList<Integer>();

		// array.add(10);
		// array.add("hello");

		// 获取字节码文件对象
		Class c = array.getClass();
		Method m = c.getMethod("add", Object.class);
		m.invoke(array, "hello");
		m.invoke(array, "world");
		m.invoke(array, "java");

		System.out.println(array);
	}
}

3、

/*
 * 反射:在运行状态下,通过class文件对象(Class的对象),去使用构造方法,成员变量,成员方法。
 * 
 * 那么,我们是如何获取到class文件对象的呢?(字节码文件对象)
 * A:Object类的getClass()方法。
 * B:数据类型的静态的class属性
 * C:通过Class类的静态方法forName(String className)
 * 
 * 
 * 开发中用第三种。自己写例子测试可以使用前两种。
 * 因为第三种方式可以结合配置文件使用。
 */
public class ReflectDemo {
	public static void main(String[] args) throws ClassNotFoundException {
		// 方式1
		Person p = new Person();
		Class c = p.getClass();

		Person p2 = new Person();
		Class c2 = p2.getClass();

		System.out.println(p == p2);// false
		System.out.println(c == c2);// true

		// 方式2
		Class c3 = Person.class;
		System.out.println(c == c3);// true

		// 方式3
		Class c4 = Class.forName("cn.itcast_01.Person");// true
		System.out.println(c == c4);
	}
}




------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值