擴展CrazyJugglerDrummer's answer
interface IAandIB extends IA, IB { }
public void Foo(IA a , IB b)
{
if (a != b)
{
throw new IllegalArgumentException () ;
}
ClassLoader loader = IAandIB . class . getClassLoader () ;
Class <?> [ ] interfaces = new Class <?> [ IAandIB . class , IA . class , IB . class ] ;
InvocationHandler handler = new InvocationHandler ()
{
public Object invoke (Object proxy , Method method , Object [ ] args) throws Exception
{
Object r = null ;
try
{
// since a==b, it should not matter whether we use a or b here.
r = method . invoke (a , args) ;
}
catch (InvocationTargetException cause)
{
throw cause . getCause () ;
}
catch (Exception cause)
{
throw new RuntimeException (cause) ;
}
}
}
IAandIB ab = (IAandIB) (Proxy . newProxyInstance (loader , interfaces , handler)) ;
Foo (ab) ;
}
public void Foo(IAandIB someClass) {
//...
}
目前您的鏈接去同一個問題,就是你故意的嗎? – 2012-01-12 21:53:40
@CrazyJugglerDrummer啊不,我不打算,謝謝! – daveslab 2012-01-12 23:29:37