服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

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


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() );
    }
  }
}

扫描关注微信公众号