体会反射的动态性
package com.reflection;
import java.util.Random;
import org.junit.Test;
/**
* 通过发射创建对应的运行时类的对象
*
*/
public class NewInstanceTest {
@Test
public void test1() throws IllegalAccessException, InstantiationException {
Class<Person> clazz = Person.class;
/**
* newInstance():调用此方法,创建对应的运行时类的对象。内部调用了运行时类的空参的构造器
*
* 要想此方法正常的创建运行时类的对象,要求:
*
* 1.运行时类必须提供空参的构造器
*
* 2.空参的构造器的访问权限得够。通常,设置为 public。
*
* 在 javabean 中要求提供一个 public 的空参构造器。原因:
*
* 1.便于通过反射,创建运行时类的对象
*
* 2.便于子类继承此运行时类,默认调用 super() 时,保证父类有此构造器
*/
Person obj = clazz.newInstance();
System.out.println(obj);
}
// 体会反射的动态性
@Test
public void test2() {
for (int i = 0; i < 100; i++) {
// 0,1,2
int num = new Random().nextInt(3);
String classPath = "";
switch (num) {
case 0:
classPath = "java.util.Date";
break;
case 1:
classPath = "java.lang.Object";
break;
case 2:
classPath = "com.reflection.Person";
break;
}
Object obj = null;
try {
obj = getInstance(classPath);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(obj);
}
}
/**
* 创建一个指定类的对象。
*
* classPath:指定类的全类名
*/
public Object getInstance(String classPath) throws Exception {
Class clazz = Class.forName(classPath);
return clazz.newInstance();
}
}