首页 > 学院 > 开发设计 > 正文

Java核心代码例程之:HelloWorld.java(2)

2019-11-18 15:14:51
字体:
来源:转载
供稿:网友

import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;

/**
 * <code>main()</code> invokes "Hello world" reflectively.
 **/

public class HelloWorld
{
  public static void main( String[] args )
  {
    Class outClass = System.out.getClass();
    Class[] argTypes = new Class[] ;

    try
    {
      Method PRintMethod = outClass.getMethod( "println", argTypes );
      printMethod.invoke( System.out, new Object[]  );
    }
    catch ( NoSUChMethodException e )
    {
      System.err.println( "Can"t find method: " + e.getMessage() );
    }
    catch ( SecurityException e )
    {
      System.err.println( "Can"t find method: " + e.getMessage() );
    }
    catch ( IllegalaccessException e )
    {
      System.err.println( "Can"t invoke method: " + e.getMessage() );
    }
    catch ( IllegalArgumentException e )
    {
      System.err.println( "Can"t invoke method: " + e.getMessage() );
    }
    catch ( InvocationTargetException e )
    {
      System.err.println( "Can"t invoke method, threw: " 
                          + e.getTargetException().getMessage() );
    }
  }
}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表