众所周知,JDK的动态代理模式必须实现接口。
以下面的源码为例:
接口一:
Java代码
1. public interface ActionInterface { 2. void doSelf(String str); 3. String getType(); |
接口二:
Java代码
1. public interface InterruupIntf { 2. void interrup(); 3. } |
实现类:
Java代码
1. public class MyselfImpl implements ActionInterface, InterruupIntf { 2. 3. public String getType() { 4. String type = "公用部门"; 5. System.out.println(type); 6. return type; 7. } 8. 9. public void doSelf(String str) { 10. System.out.println("业务实现类: " + str + " !"); 11. } 12. 13. public void interrup() { 14. System.out.println("发呆三分钟!"); 15. } 16. } |
动态代理需要反射,
* 必须要实现InvocationHandler接口
* 能够对所有的方法进行代理