Using reflection in Java to create a new instance with the reference variable type set to the new instance class name?

后端 未结 6 1253
忘掉有多难
忘掉有多难 2020-12-03 03:30

All the examples I look at for reflection show creating a new instance of an unknown implementation, and casting that implementation to it\'s interface. The issue with this

6条回答
  •  -上瘾入骨i
    2020-12-03 03:36

    //====Single Class Reference used to retrieve object for fields and initial values. Performance enhancing only====          
    Class    reference           =   vector.get(0).getClass();
    Object      obj                 =   reference.newInstance();
    Field[]     objFields           =   obj.getClass().getFields(); 
    

提交回复
热议问题