| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 
 | import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Proxy;
 
 public class DynamicProxy {
 
 
 public static void main(String[] args) {
 
 Student ordinaryStudents = new OrdinaryStudents();
 ordinaryStudents.eat();
 ordinaryStudents.write();
 
 
 
 
 
 
 
 
 
 InvocationHandler handler = (proxy, method, handlerArgs) -> {
 
 if ("eat".equals(method.getName())) {
 System.out.println("我可以吃香喝辣!");
 return null;
 }
 
 if ("write".equals(method.getName())) {
 System.out.println("我的作文题目是《我的区长父亲》。");
 
 method.invoke(ordinaryStudents, handlerArgs);
 System.out.println("我的作文拿了区作文竞赛一等奖!so easy!");
 return null;
 }
 return null;
 };
 
 
 
 
 
 
 Student sonOfDistrict = (Student) Proxy.newProxyInstance(ordinaryStudents.getClass().getClassLoader(), ordinaryStudents.getClass().getInterfaces(), handler);
 sonOfDistrict.eat();
 sonOfDistrict.write();
 
 }
 }
 
 
 
 
 
 interface Student {
 
 void eat();
 
 void run();
 
 void write();
 }
 
 
 
 class OrdinaryStudents implements Student {
 @Override
 public void eat() {
 System.out.println("我在吃饭!");
 }
 
 @Override
 public void run() {
 System.out.println("我在跑步!");
 }
 @Override
 public void write() {
 System.out.println("我在写作文!");
 }
 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 |