您的位置:首页 > 职场人生

黑马程序员——反射

2015-08-13 12:16 591 查看
------- <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:Class类forName()静态方法

 

    推荐:开发使用第三种。

 

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

(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.print
98f1
ln(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>、期待与您交流! ----------


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: