java如何用反射获取类实例?
想了解更多Java相关,百度搜索圈T社区,免费视频教程。纯干货
public class Demo {private String key1 = "1";private String key2 = "2";public String getKey1() {return key1;}public void setKey1(String key1) {this.key1 = key1;}public String getKey2() {return key2;}public void setKey2(String key2) {this.key2 = key2;}public static void main(String[] args) throws Exception {//参数 “Demo” 是类的全名,如果在包结构下,要有完整包路径 比如: com.test.DemoClass<?> clazz = Class.forName("Demo");//“Demo”类必须有默认构造方法,否则会抛出异常Demo demo = (Demo) clazz.newInstance();System.out.println(demo.getKey1());System.out.println(demo.getKey2());}}