学习使用反射创建运行时类对象
package cto;
import org.junit.Test;
public class TestClass {
@Test
/**
* 通过反射创建运行类对象
* 1.需要有一个无参的构造器
* 2.构造器的权限修饰符不能为private
* @throws Exception
*/
public void test() throws Exception{
Class clazz = Class.forName("cto.MyDate");
//创建运行时的类对象
MyDate d =(MyDate) clazz.newInstance();
System.out.println(d);
}
}