Dynamically loading a class in Java

后端 未结 3 2038
时光取名叫无心
时光取名叫无心 2020-12-06 04:37

I looked up the syntax and searched the api but am still confused about the process. I also searched Stackoverflow. What is the proper way to load a class and create an obj

3条回答
  •  难免孤独
    2020-12-06 05:07

    Here is what I got working. This is not a finihsed product, but is just test to see if I could get it to work. Thank you to everyone that answered the questoin :-).

    public class SimLoader {  
      public static void main(String[] args)  
      {  
        try  
        {  
        Object simulator = Class.forName("SimX").newInstance();  
        ((SimInterface)simulator).run();  
        }  
        catch(ClassNotFoundException e) {}  
        catch(InstantiationException e) {}  
        catch(IllegalAccessException e) {}  
        }  
      }  
    interface SimInterface {  
     void run();  
    }  
    class SimX implements SimInterface  
    {  
      public void run() {  
        System.out.println("Success");  
      }  
    }  
    

提交回复
热议问题