我有两个类A和B都实现了接口Z。现在,对于接口Z的某些功能(Z.f1,Z.f2,Z.f3等),类A应该只作为对象的分派器工作。 B级
public class A implements Z{
private B b; //instantiated in constructor of A
@Override
public String f1(int p)
{
return b.f1(p);
}
...
在Java中有通用的方法吗?
最佳答案
如果您要在接口f1()
中声明方法Z
,则要实现的模式称为包装器或装饰器。
在Java中,您可以使用Java 1.4中引入的动态代理来创建通用实现。
关于java - 在Java中使用相同的接口(interface)向其他类调度函数调用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13126638/