{"id":221955,"date":"2021-07-18T10:16:21","date_gmt":"2021-07-18T02:16:21","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=221955"},"modified":"2021-07-07T11:16:51","modified_gmt":"2021-07-07T03:16:51","slug":"java-generic-erase","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/java-generic-erase.html","title":{"rendered":"\u5341\u5206\u949f\u7406\u89e3Java\u6cdb\u578b\u64e6\u9664"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u6cdb\u578b\u4fe1\u606f\u53ea\u5b58\u5728\u4e8e\u4ee3\u7801\u7f16\u8bd1\u9636\u6bb5\uff0c\u4f46\u662f\u5728java\u7684\u8fd0\u884c\u671f(\u5df2\u7ecf\u751f\u6210\u5b57\u8282\u7801\u6587\u4ef6\u540e)\u4e0e\u6cdb\u578b\u76f8\u5173\u7684\u4fe1\u606f\u4f1a\u88ab\u64e6\u9664\u6389\uff0c\u4e13\u4e1a\u672f\u8bed\u53eb\u505a\u7c7b\u578b\u64e6\u9664\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u4eca\u5929\u6211\u4eec\u6765\u8bb2\u89e3\u6cdb\u578b\u4e2d\u53e6\u4e00\u4e2a\u91cd\u8981\u77e5\u8bc6\u70b9\u2014\u2014\u6cdb\u578b\u64e6\u9664!
\n\"\"<\/p>\n

\u6cdb\u578b\u64e6\u9664\u6982\u5ff5<\/strong><\/div>\n

\u6cdb\u578b\u4fe1\u606f\u53ea\u5b58\u5728\u4e8e\u4ee3\u7801\u7f16\u8bd1\u9636\u6bb5\uff0c\u4f46\u662f\u5728java\u7684\u8fd0\u884c\u671f(\u5df2\u7ecf\u751f\u6210\u5b57\u8282\u7801\u6587\u4ef6\u540e)\u4e0e\u6cdb\u578b\u76f8\u5173\u7684\u4fe1\u606f\u4f1a\u88ab\u64e6\u9664\u6389\uff0c\u4e13\u4e1a\u672f\u8bed\u53eb\u505a\u7c7b\u578b\u64e6\u9664\u3002\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n

\r\nArrayList l1 = new ArrayList(); \r\nArrayList l2 = new ArrayList();  \r\nSystem.out.println(l1.getClass()==l2.getClass()); \r\n<\/string><\/integer><\/pre>\n

\u8fd0\u884c\u4ee3\u7801\uff0c\u7ed3\u679c\u4e3aTrue<\/p>\n

\u8fd9\u662f\u56e0\u4e3aArrayList \u548cArrayList \u5728 jvm \u4e2d\u7684 Class \u90fd\u662f List.class\uff0c\u4e8c\u8005\u5728 jvm \u4e2d\u7b49\u540c\u4e8eList\u3002<\/p>\n

\u901a\u8fc7\u4e0b\u9762\u7684\u4f8b\u5b50\u6211\u4eec\u505a\u8fdb\u4e00\u6b65\u7684\u5206\u6790<\/p>\n

\r\nimport java.lang.reflect.Field; \r\npublic class GeneErasure { \r\n   T object; \r\n   public GeneErasure(T object) { \r\n      this.object = object; \r\n   } \r\n   public static void main(String[] args) { \r\n      GeneErasure demo = new GeneErasure(\"hi\"); \r\n      Class classz = demo.getClass(); \r\n      System.out.println(classz.getName()); \r\n      \/\/\u8f93\u51facom.my.generic.GeneErasure \r\n      Field[] fs = classz.getDeclaredFields(); \r\n      for ( Field f:fs) { \r\n        System.out.println(\"feild: \"+f.getName()+\"type:\"+f.getType().getName()); \r\n        \/\/\u8f93\u51fafeild: object type:java.lang.Object \r\n      } \r\n   } \r\n<\/string><\/t><\/pre>\n

\u901a\u8fc7\u8fd9\u4e2a\u4f8b\u5b50\u6211\u4eec\u53ef\u4ee5\u770b\u5230Class \u7684\u7c7b\u578b\u4ecd\u7136\u662fGeneErasure\u5e76\u4e0d\u662fGeneErasure \u8fd9\u79cd\u5f62\u5f0f\uff0c\u800c\u7c7b\u578bT\u88ab\u66ff\u6362\u6210 Object \u7c7b\u578b\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u505a\u53e6\u4e00\u4e2a\u5c1d\u8bd5\uff0c\u628aGeneErasure<\/t> \u66f4\u6539\u4e3a GeneErasure < T extends String><\/p>\n

\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n

\r\nfeild: object type:java.lang.String \r\n<\/pre>\n

\u6240\u4ee5\uff0c\u5728\u6cdb\u578b\u7c7b\u88ab\u7c7b\u578b\u64e6\u9664\u7684\u65f6\u5019\uff0c\u4e4b\u524d\u6cdb\u578b\u7c7b\u4e2d\u7684\u7c7b\u578b\u53c2\u6570\u90e8\u5206\u5982\u679c\u6ca1\u6709\u6307\u5b9a\u4e0a\u9650\uff0c\u5982<\/t>\u5219\u4f1a\u88ab\u8f6c\u8bd1\u6210\u666e\u901a\u7684Object \u7c7b\u578b\uff0c\u5982\u679c\u6307\u5b9a\u4e86\u4e0a\u9650\uff0c\u5982<\/t>\u5219\u7c7b\u578b\u53c2\u6570\u5c31\u88ab\u66ff\u6362\u6210\u7c7b\u578b\u4e0a\u9650\u3002<\/p>\n

\u5229\u7528\u7c7b\u578b\u64e6\u9664\u5e72\u201c\u574f\u4e8b\u513f\u201d<\/strong><\/div>\n

\u5927\u5bb6\u90fd\u77e5\u9053\uff0c\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801l.add(123)\u65e0\u6cd5\u7f16\u8bd1\u901a\u8fc7\uff0c\u56e0\u4e3a123\u4e0d\u662fString\u7c7b\u578b\uff0c\u8fd9\u4e5f\u662f\u4f7f\u7528\u6cdb\u578b\u7684\u597d\u5904\u4e4b\u4e00\u3002<\/p>\n

\r\nArrayList l=new ArrayList<\/string>(); \r\n \r\nl.add(\"abc\"); \r\n \r\nl.add(123); \r\n<\/string><\/pre>\n

\u4f46\u662f\u6211\u4eec\u7406\u89e3\u4e86\u6cdb\u578b\u64e6\u9664\u7684\u539f\u7406\uff0c\u6211\u4eec\u53ef\u4ee5\u5de7\u5999\u5730\u5229\u7528\u8fd9\u4e2a\u539f\u7406\u7ed3\u5408\u53cd\u5c04\u77e5\u8bc6\u5e72\u4e00\u4e9b\u201c\u574f\u4e8b\u201d\uff0c\u4f8b\u5982\uff1a<\/p>\n

\r\nArrayListl=new ArrayList<\/string>(); \r\n     l.add(\"abc\");    \r\n     try { \r\n       Method method = l.getClass().getDeclaredMethod(\"add\",Object.class); \r\n       method.invoke(l,\"test\"); \r\n       method.invoke(l,100.f); \r\n     }catch (Exception e) { \r\n       e.printStackTrace(); \r\n     } \r\n     System.out.println(\"list\u7684\u5927\u5c0f\u662f\uff1a\"+l.size());  \r\n     for ( Object o: l){ \r\n       System.out.println(o); \r\n     }\r\n<\/string><\/pre>\n

\u8fd0\u884c\u7ed3\u679c\u662f\uff1a<\/p>\n

\r\nlist\u7684\u5927\u5c0f\u662f\uff1a3 \r\nabc \r\ntest \r\n100.0(\u88ab\u6210\u529f\u63d2\u5165\u5230ArrayList\u4e2d) \r\n<\/pre>\n

\u6211\u4eec\u53ef\u4ee5\u770b\u89c1100.0 \u6210\u529f\u5730\u63d2\u5165\u5230ArrayList \u4e2d\u4e86\uff0c\u6240\u4ee5\u5229\u7528\u7c7b\u578b\u64e6\u9664\u7684\u539f\u7406\u5e76\u7ed3\u5408\u53cd\u5c04\u7684\u624b\u6bb5\u5c31\u7ed5\u8fc7\u4e86\u6b63\u5e38\u5f00\u53d1\u4e2d\u7f16\u8bd1\u5668\u4e0d\u5141\u8bb8\u7684\u64cd\u4f5c\u9650\u5236\u3002<\/p>\n

\u901a\u4fd7\u7684\u7406\u89e3<\/strong><\/div>\n

\u6211\u4eec\u53ef\u4ee5\u5c06\u6cdb\u578b\u6bd4\u4f5c\u662f\u4e00\u4e2a\u770b\u5b88\uff0c\u4ed6\u6765\u5b88\u62a4\u6211\u4eec\u7684\u4ee3\u7801\u5b89\u5168\uff0c\u7136\u540e\u8bbe\u7f6e\u5404\u9879\u89c4\u5b9a\uff0c\u201cxxx \u7981\u6b62\u51fa\u5165\u201d\u7684\u63d0\u9192\u3002\u800c\u73b0\u5b9e\u751f\u6d3b\u4e2d\uff0c\u4e5f\u603b\u4f1a\u6709\u4e9b\u4eba\u80fd\u591f\u57fa\u4e8e\u5bf9\u95e8\u536b\u4eec\u751f\u6d3b\u4f5c\u606f\u7684\u89c4\u5f8b\uff0c\u7ed5\u5f00\u4ed6\u4eec\u7684\u76d1\u89c6(\u53cd\u5c04\u7ed3\u5408\u6cdb\u578b\u64e6\u9664)\u6765\u5e72\u4e00\u4e9b\u574f\u4e8b\u513f \u3002<\/string><\/t><\/object><\/integer><\/string><\/p>\n","protected":false},"excerpt":{"rendered":"

\u4eca\u5929\u6211\u4eec\u6765\u8bb2\u89e3\u6cdb\u578b\u4e2d\u53e6\u4e00\u4e2a\u91cd\u8981\u77e5\u8bc6\u70b9\u2014\u2014\u6cdb\u578b\u64e6\u9664! \u6cdb\u578b\u4fe1\u606f\u53ea\u5b58\u5728\u4e8e\u4ee3\u7801\u7f16\u8bd1\u9636\u6bb5\uff0c\u4f46\u662f\u5728java\u7684\u8fd0\u884c\u671f(\u5df2\u7ecf […]<\/p>\n","protected":false},"author":1898,"featured_media":221960,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[710],"class_list":["post-221955","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-java"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/221955","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=221955"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/221955\/revisions"}],"predecessor-version":[{"id":221959,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/221955\/revisions\/221959"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/221960"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=221955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=221955"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=221955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}