Java反射创建对象的几种方式
获取Class对象:
方法一
Class studentClass=类名.class;
方法二
Class studentClass=Class.forName("包完整类名");
方法三
Student student=new Student();
Class studentClass=student.getClass();
Class对象获取对象的方法:
/*通过无参构造获取对象*/
Student student1=(Student) studentClass.newInstance();
/*通过有参构造方法获取对象*/
studentClass.getConstructor(String.class,Integer.class);
Student student24=(Student) constructor.newInstance("Zhang",21);
还没有评论,来说两句吧...