重写类实例的方法?

发布于 2024-12-18 01:36:24 字数 501 浏览 1 评论 0原文

是否可以反射性地重写类的给定实例的方法?

前提条件:游戏有一个可重写的方法 act()

public class Foo {

  public Method[] getMethods() {
    Class s = Game.class;
    return s.getMethods();
  }

  public void override()
  {
    Method[] arr = getMethods()
    for (int i = 0; i<arr.length; i++)
    {
      if (arr[i].toGenericString().contains("act()")
      {
        // code to override method (it can just disable to method for all i care)
      }
    }  
  }                  
}

Is it possible to reflectively override a method for a given instance of a class?

Precondition: Game has an override-able method act().

public class Foo {

  public Method[] getMethods() {
    Class s = Game.class;
    return s.getMethods();
  }

  public void override()
  {
    Method[] arr = getMethods()
    for (int i = 0; i<arr.length; i++)
    {
      if (arr[i].toGenericString().contains("act()")
      {
        // code to override method (it can just disable to method for all i care)
      }
    }  
  }                  
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情绪操控生活 2024-12-25 01:36:24

如果 Game 是一个接口或使用 act() 方法实现接口,您可以使用 代理 。如果接口很小,最优雅的方法可能是使用装饰器设计模式创建一个实现它的类。

If Game is an interface or implements an interface with the method act() you can use Proxy for that. If the interface is small, the most elegant way would probably be to create a class implementing it employing the Decorator design pattern.

一腔孤↑勇 2024-12-25 01:36:24

使用纯 Java 动态重写类的方法是不可能的。 第三方库cglib可以创建动态子类。您可能想检查一下。

如果您可以针对接口进行编码,那么您可以使用 Java动态代理创建一个覆盖行为的代理对象,如下例所示。假设 Game 正在实现接口 IGame

class GameInvocationHandler implements InvocationHandler
{
    private Game game;
    public GameInvocationHandler(Game game)
    {
        this.game = game;
    }
    Object invoke(Object proxy, Method method, Object[] args)
    {
        if (method.toGenericString().contains("act()")
        {
            //do nothing;
            return null;
        }
        else
        {
            return method.invoke(game, args);
        }
    }
}

Class proxyClass = Proxy.getProxyClass(Foo.class.getClassLoader(), IGame.class);
IGame f = (IGame) proxyClass.getConstructor(InvocationHandler.class).newInstance(new Object[] {  });

It is not possible to dynamically override methods of a class using pure Java. The third-party library cglib can create dynamic subclasses. You may want to check that out.

If you can code against interface, then you may use Java dynamic proxy to create a proxy object which overrides the behavior, like the example below. Suppose Game was implementing an interface IGame.

class GameInvocationHandler implements InvocationHandler
{
    private Game game;
    public GameInvocationHandler(Game game)
    {
        this.game = game;
    }
    Object invoke(Object proxy, Method method, Object[] args)
    {
        if (method.toGenericString().contains("act()")
        {
            //do nothing;
            return null;
        }
        else
        {
            return method.invoke(game, args);
        }
    }
}

Class proxyClass = Proxy.getProxyClass(Foo.class.getClassLoader(), IGame.class);
IGame f = (IGame) proxyClass.getConstructor(InvocationHandler.class).newInstance(new Object[] {  });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文