Class and static method Class.forName() drive me crazy

后端 未结 5 1349
梦毁少年i
梦毁少年i 2020-12-30 08:51

this code doesn\'t compile. I\'m wondering what I am doing wrong:

private static Importable getRightInstance(String s) throws Exception {
 Class

        
5条回答
  •  伪装坚强ぢ
    2020-12-30 09:27

    Use a runtime conversion:

    Class   c
        = Class.forName (s).asSubclass (Importable.class);
    

    This will bark with an exception at runtime if s specifies a class that doesn't implement the interface.

提交回复
热议问题