





{"id":1839,"date":"2011-04-11T01:27:10","date_gmt":"2011-04-11T05:27:10","guid":{"rendered":"http:\/\/www.killtest.hk\/?p=1839"},"modified":"2011-04-11T01:27:10","modified_gmt":"2011-04-11T05:27:10","slug":"java-3","status":"publish","type":"post","link":"https:\/\/www.killtest.hk\/index.php\/unix\/java-3\/","title":{"rendered":"\u4ee3\u7406\u6a21\u5f0f\u4e4bJava\u52d5\u614b\u4ee3\u7406"},"content":{"rendered":"<p>Proxy:\u5b8c\u5168\u7531java\u7522\u751f\u7684\uff0c\u800c\u4e14\u5be6\u73fe\u4e86\u5b8c\u6574\u7684subject\u63a5\u53e3\u3002<br \/>\n\u00a0\u00a0\u00a0 InvocationHandler\uff1aProxy\u4e0a\u7684\u4efb\u4f55\u65b9\u6cd5\u8abf\u7528\u90fd\u6703\u88ab\u50b3\u5165\u6b64\u985e,InvocationHandler\u63a7\u5236\u5c0dRealSubject\u7684\u8a2a\u554f\u3002<br \/>\n\u00a0\u00a0\u00a0 \u56e0\u70baJava\u5df2\u7d93\u5e6b\u52a9\u6211\u5011\u5275\u5efa\u4e86Proxy\u985e\uff0c\u6211\u5011\u9700\u8981\u6709\u8fa6\u6cd5\u544a\u8a34Proxy\u985e\u59b3\u8981\u505a\u4ec0\u9ebd\uff0c\u6211\u5011\u4e0d\u80fd\u50cf\u4ee5\u524d\u58f9\u6a23\u628a\u4ee3\u78bc\u5beb\u5165\u5230Proxy\u985e\u4e2d\uff0c\u56e0\u70baProxy\u985e\u4e0d\u662f\u6211\u5011\u5be6\u73fe\u7684\u3002\u90a3\u9ebd\u6211\u5011\u61c9\u8a72\u653e\u5728\u54ea\u88cf?\u653e\u5728InvocationHandler\u985e\u4e2d\uff0cInvocationHandler\u985e\u662f\u97ff\u61c9\u4ee3\u7406\u7684\u4efb\u4f55\u8abf\u7528\u3002\u6211\u5011\u53ef\u4ee5\u5427InvocationHandler\u60f3\u6210\u662f\u4ee3\u7406\u6536\u5230\u65b9\u6cd5\u8abf\u7528\u5f8c\uff0c\u8acb\u6c42\u505a\u5be6\u969b\u5de5\u4f5c\u7684\u5c0d\u8c61\u3002<br \/>\n\u00a0\u00a0\u00a0 2. java.lang.reflect.InvocationHandler<br \/>\n\u00a0\u00a0\u00a0 \u88ab\u4ee3\u7406\u5be6\u4f8b\u6240\u5be6\u73fe\u7684\u58f9\u500b\u63a5\u53e3\uff0c\u5167\u90e8\u53ea\u6709\u58f9\u500binvoke()\u65b9\u6cd5,\u7c3d\u540d\u5982\u4e0b;<br \/>\n\u00a0\u00a0\u00a0 Java\u4ee3\u78bc<br \/>\n\u00a0\u00a0\u00a0 public Object invoke(Object proxy, Method method, Object[] args)<br \/>\n\u00a0\u00a0\u00a0 \u7576\u4ee3\u7406\u7684\u65b9\u6cd5\u88ab\u8abf\u7528\u7684\u6642\u5019\uff0c\u4ee3\u7406\u5c31\u6703\u628a\u9019\u500b\u8abf\u7528\u8f49\u767c\u7d66InvocationHandler\uff0c\u4e5f\u5c31\u6703\u8abf\u7528\u5b83\u7684invoke()\u65b9\u6cd5\u3002<br \/>\n\u00a0\u00a0\u00a0 3. java.lang.reflect.Proxy<br \/>\n\u00a0\u00a0\u00a0 \u63d0\u4f9b\u7528\u65bc\u5275\u5efa\u52d5\u614b\u4ee3\u7406\u985e\u548c\u5be6\u4f8b\u7684\u975c\u614b\u65b9\u6cd5\uff0c\u5b83\u9084\u662f\u7531\u9019\u4e9b\u65b9\u6cd5\u5275\u5efa\u7684\u6240\u6709\u52d5\u614b\u4ee3\u7406\u985e\u7684\u8d85\u985e\uff0c\u6211\u5011\u7d93\u5e38\u4f7f\u7528\u7684\u975c\u614b\u65b9\u5f0f\u662f\uff1a<br \/>\n\u00a0\u00a0\u00a0 Java\u4ee3\u78bc<br \/>\n\u00a0\u00a0\u00a0 newProxyInstance(ClassLoader loader, Class[] interfaces, InvocationHandler h)<br \/>\n\u00a0\u00a0\u00a0 4. \u793a\u4f8b\uff1a<br \/>\n\u00a0\u00a0\u00a0 \u60c5\u5f62\uff1a\u81ea\u5df1\u53ef\u4ee5\u67e5\u770b\u4fee\u6539\u59d3\u540d\u6027\u5225\uff0c\u4f46\u662f\u4e0d\u80fd\u4fee\u6539rate\u3002\u4ed6\u4eba\u53ef\u4ee5\u67e5\u770b\u59d3\u540d\uff0c\u6027\u5225\u4ee5\u53ca\u4fee\u6539rate\uff0c\u4f46\u662f\u4e0d\u80fd\u4fee\u6539\u59d3\u540d\u6027\u5225\u3002<br \/>\n\u00a0\u00a0\u00a0 4.1 \u5b9a\u7fa9\u58f9\u500b\u63a5\u53e3:<br \/>\n\u00a0\u00a0\u00a0 Java\u4ee3\u78bc<br \/>\n\u00a0\u00a0\u00a0 public interface Person {<br \/>\n\u00a0\u00a0\u00a0 String getName();<br \/>\n\u00a0\u00a0\u00a0 String getGender();<br \/>\n\u00a0\u00a0\u00a0 void setName(String name);<br \/>\n\u00a0\u00a0\u00a0 void setGender(String gender);<br \/>\n\u00a0\u00a0\u00a0 void setRate(int rate);<br \/>\n\u00a0\u00a0\u00a0 int getRate();<br \/>\n\u00a0\u00a0\u00a0 }<br \/>\n\u00a0\u00a0\u00a0 4.2 \u5b9a\u7fa9\u5be6\u73fePerson\u63a5\u53e3\u985e<br \/>\n\u00a0\u00a0\u00a0 Java\u4ee3\u78bc<br \/>\n\u00a0\u00a0\u00a0 public class PersonImpl implements Person {<br \/>\n\u00a0\u00a0\u00a0 String name;<br \/>\n\u00a0\u00a0\u00a0 String gender;<br \/>\n\u00a0\u00a0\u00a0 String interests;<br \/>\n\u00a0\u00a0\u00a0 int rate;<br \/>\n\u00a0\u00a0\u00a0 public String getName() {<br \/>\n\u00a0\u00a0\u00a0 return name;<br \/>\n\u00a0\u00a0\u00a0 }<br \/>\n\u00a0\u00a0\u00a0 public void setName(String name) {<br \/>\n\u00a0\u00a0\u00a0 this.name = name;<br \/>\n\u00a0\u00a0\u00a0 }<br \/>\n\u00a0\u00a0\u00a0 public String getGender() {<br \/>\n\u00a0\u00a0\u00a0 return gender;<br \/>\n\u00a0\u00a0\u00a0 }<br \/>\n\u00a0\u00a0\u00a0 public void setGender(String gender) {<br \/>\n\u00a0\u00a0\u00a0 this.gender = gender;<br \/>\n\u00a0\u00a0\u00a0 }<br \/>\n\u00a0\u00a0\u00a0 public String getInterests() {<br \/>\n\u00a0\u00a0\u00a0 return interests;<br \/>\n\u00a0\u00a0\u00a0 }<br \/>\n\u00a0\u00a0\u00a0 public void setInterests(String interests) {<br \/>\n\u00a0\u00a0\u00a0 this.interests = interests;<br \/>\n\u00a0\u00a0\u00a0 }<br \/>\n\u00a0\u00a0\u00a0 public int getRate() {<br \/>\n\u00a0\u00a0\u00a0 return rate;<br \/>\n\u00a0\u00a0\u00a0 }<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Proxy:\u5b8c\u5168\u7531java\u7522\u751f\u7684\uff0c\u800c\u4e14\u5be6\u73fe\u4e86\u5b8c\u6574\u7684subject\u63a5\u53e3\u3002 \u00a0\u00a0\u00a0 InvocationHandl [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[531],"tags":[331,763,764],"class_list":["post-1839","post","type-post","status-publish","format-standard","hentry","category-unix","tag-java","tag-763","tag-764"],"_links":{"self":[{"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/posts\/1839","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/comments?post=1839"}],"version-history":[{"count":0,"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/posts\/1839\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/media?parent=1839"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/categories?post=1839"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/tags?post=1839"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}