是否可以反射地覆盖给定类的实例的方法?
前提条件:游戏具有覆盖能力的方法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)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果Game是接口或使用该方法实现接口,则act()
可以使用Proxy.如果界面很小,最优雅的方法可能是创建一个使用Decorator设计模式实现它的类.
归档时间: |
|
查看次数: |
4738 次 |
最近记录: |