【发布时间】:2015-08-12 08:32:42
【问题描述】:
我知道我们可以使用 cglib 代理类。我想知道为什么JDK动态代理只能通过接口代理。我已经阅读了源代码。
/*
* Verify that the Class object actually represents an
* interface.
*/
if (!interfaceClass.isInterface()) {
throw new IllegalArgumentException(
interfaceClass.getName() + " is not an interface");
}
在Proxy.newProxyInstance(loader, interfaces, h)方法中,会检查条件。 我想知道为什么必须是接口?这是标准?
【问题讨论】:
-
当然这是标准。这就是它在Javadoc 中所说的。这个问题毫无意义。如果您希望它知道为什么它是标准,这是一个更有趣的问题,但不是您可以合理地期望在 Java 开发团队之外的任何地方得到答案。