invoke.java

来自「learning java的源代码。书中每个实例都有相关的代码example。」· Java 代码 · 共 27 行

JAVA
27
字号
//file: Invoke.javaimport java.lang.reflect.*;class Invoke {  public static void main( String [] args ) {    try {      Class c = Class.forName( args[0] );      Method m = c.getMethod( args[1], new Class [] { } );      Object ret =  m.invoke( null, null );      System.out.println(          "Invoked static method: " + args[1]          + " of class: " + args[0]          + " with no args\nResults: " + ret );    } catch ( ClassNotFoundException e ) {      // Class.forName(  ) can't find the class    } catch ( NoSuchMethodException e2 ) {      // that method doesn't exist    } catch ( IllegalAccessException e3 ) {      // we don't have permission to invoke that method    } catch ( InvocationTargetException e4 ) {      // an exception ocurred while invoking that method      System.out.println(          "Method threw an: " + e4.getTargetException(  ) );    }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?