Create an instance within Abstract Class using Reflection

前端 未结 3 1892
一整个雨季
一整个雨季 2021-01-19 00:45

Is it possible to create an instance of a derived class in abstract ancestor class using reflection Lets say:

abstract class Base {

public Base createInstan         


        
3条回答
  •  自闭症患者
    2021-01-19 01:30

    You can use Class.forName() and Class.newInstance() to create any class. But there's no way to easily identify subclasses of a class. See this JavaWorld tip for a technique to do this.

    I think , however, the real issue is what is it you're ultimately trying to achieve, and can it be done more easily using conventional techniques.

提交回复
热议问题