当前位置: 代码迷 >> 综合 >> Java反射创建对象的几种方式
  详细解决方案

Java反射创建对象的几种方式

热度:80   发布时间:2024-02-24 13:14:12.0

获取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);