methodref.java

来自「java的简单例子」· Java 代码 · 共 40 行

JAVA
40
字号
package jws.lang;

// Copyright 1997, John Webster Small
// All rights Reserved

import java.lang.reflect.*;

public final class MethodRef
{
  private final Object ref;
  private final Method method;

  public MethodRef
    (Object ref, String name, Class[] parameterTypes)
    throws NoSuchMethodException, SecurityException
  {
    this.ref = ref;
    method = ref.getClass().getMethod(name,parameterTypes);
  }

  public MethodRef(Object ref, String name)
    throws NoSuchMethodException, SecurityException
  {
    this.ref = ref;
    method = ref.getClass().getMethod(name,new Class[0]);
  }

  public final Object invoke(Object[] args)
    throws IllegalAccessException,
      IllegalArgumentException,
      InvocationTargetException
    { return method.invoke(ref,args); }

  public final Object invoke()
    throws IllegalAccessException,
      IllegalArgumentException,
      InvocationTargetException
    { return method.invoke(ref,new Object[0]); }
}

⌨️ 快捷键说明

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