首页 > 代码库 > Method类的 invoke()方法

Method类的 invoke()方法

 1 public class MethodTest 2  3 { 4     public static void main(String[] args) 5     { 6         String [] names ={"tom","tim","allen","alice"}; 7         Class<?> clazz = Test.class; 8         try 9         {10             Method method = clazz.getMethod("sayHi", String.class);11             for(String name:names)12                 method.invoke(clazz.newInstance(),name);13         } catch (NoSuchMethodException e)14         {15             e.printStackTrace();16         } catch (IllegalAccessException e)17         {18             e.printStackTrace();19         } catch (IllegalArgumentException e)20         {21             e.printStackTrace();22         } catch (InvocationTargetException e)23         {24             e.printStackTrace();25         } catch (InstantiationException e)26         {27             e.printStackTrace();28         }29     }30 }31 class Test32 {33     public void sayHi(String name)34     {35         System.out.println("Hi "+name);36     }37 }

 

Method类的 invoke()方法