{"id":1058,"date":"2017-02-14T13:37:38","date_gmt":"2017-02-14T05:37:38","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=1058"},"modified":"2017-02-14T13:37:38","modified_gmt":"2017-02-14T05:37:38","slug":"%e6%b3%9b%e5%9e%8b%e4%b8%ad-super-t-%e5%92%8c-extends-t-%e7%9a%84%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=1058","title":{"rendered":"\u6cdb\u578b\u4e2d ? super T \u548c ? extends T \u7684\u533a\u522b"},"content":{"rendered":"<p>\n\t&lt;? super T&gt;\u8868\u793a\u5305\u62ecT\u5728\u5185\u7684\u4efb\u4f55T\u7684\u7236\u7c7b\uff0c&lt;? extends T&gt;\u8868\u793a\u5305\u62ecT\u5728\u5185\u7684\u4efb\u4f55T\u7684\u5b50\u7c7b\uff0c\u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u5206\u6790\u8fd9\u4e24\u79cd\u901a\u914d\u7b26\u7684\u533a\u522b\n<\/p>\n<h2>\n\textends<br \/>\n<\/h2>\n<p>\n\tList&lt;? extends Number&gt; foo3\u7684\u901a\u914d\u7b26\u58f0\u660e\uff0c\u610f\u5473\u7740\u4ee5\u4e0b\u7684\u8d4b\u503c\u90fd\u662f\u5408\u6cd5\u7684\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\n\/\/ Number &quot;extends&quot; Number (in this context)\r\n\r\nList&lt;? extends Number&gt; foo3 = new ArrayList&lt;? extends Number&gt;(); \r\n\r\n\/\/ Integer extends Number\r\n\r\nList&lt;? extends Number&gt; foo3 = new ArrayList&lt;? extends Integer&gt;();\r\n\r\n\/\/ Double extends Number\r\n\r\nList&lt;? extends Number&gt; foo3 = new ArrayList&lt;? extends Double&gt;();<\/pre>\n<p>\n\t\u8bfb\u53d6\u64cd\u4f5c\u901a\u8fc7\u4ee5\u4e0a\u7ed9\u5b9a\u7684\u8d4b\u503c\u8bed\u53e5\uff0c\u4e00\u5b9a\u80fd\u4ecefoo3\u5217\u8868\u4e2d\u8bfb\u53d6\u5230\u7684\u5143\u7d20\u7684\u7c7b\u578b\u662f\u4ec0\u4e48\uff1f\n<\/p>\n<ul>\n<li>\n\t\t\u80fd\u8bfb\u53d6\u5230Number\uff0c\u56e0\u4e3a\u4ee5\u4e0a\u7684\u5217\u8868\u8981\u4e48\u5305\u542bNumber\u5143\u7d20\uff0c\u8981\u4e48\u5305\u542bNumber\u7684\u7c7b\u5143\u7d20\u3002\n\t<\/li>\n<li>\n\t\t\u4e0d\u80fd\u4fdd\u8bc1\u8bfb\u53d6\u5230Integer\uff0c\u56e0\u4e3afoo3\u53ef\u80fd\u6307\u5411\u7684\u662fList&lt;Double&gt;\u3002\n\t<\/li>\n<li>\n\t\t\u4e0d\u80fd\u4fdd\u8bc1\u8bfb\u53d6\u5230Double\uff0c\u56e0\u4e3afoo3\u53ef\u80fd\u6307\u5411\u7684\u662fList&lt;Integer&gt;\u3002\n\t<\/li>\n<\/ul>\n<p>\n\t\u5199\u5165\u64cd\u4f5c\u901a\u8fc7\u4ee5\u4e0a\u7ed9\u5b9a\u7684\u8d4b\u503c\u8bed\u53e5\uff0c\u80fd\u628a\u4ec0\u4e48\u7c7b\u578b\u7684\u6570\u636e\u5408\u6cd5\u63d2\u5165\u5230foo3\u4e2d\uff1f\n<\/p>\n<ul>\n<li>\n\t\t\u4e0d\u80fd\u63d2\u5165\u4e00\u4e2aInteger\u5143\u7d20\uff0c\u56e0\u4e3afoo3\u53ef\u80fd\u6307\u5411List&lt;Double&gt;\u3002\n\t<\/li>\n<li>\n\t\t\u4e0d\u80fd\u63d2\u5165\u4e00\u4e2aDouble\u5143\u7d20\uff0c\u56e0\u4e3afoo3\u53ef\u80fd\u6307\u5411List&lt;Integer&gt;\u3002\n\t<\/li>\n<li>\n\t\t\u4e0d\u80fd\u63d2\u5165\u4e00\u4e2aNumber\u5143\u7d20\uff0c\u56e0\u4e3afoo3\u53ef\u80fd\u6307\u5411List&lt;Integer&gt;\u3002\n\t<\/li>\n<\/ul>\n<p>\n\t\u4e0d\u80fd\u5f80List&lt;? extends T&gt;\u4e2d\u63d2\u5165\u4efb\u4f55\u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u56e0\u4e3a\u4e0d\u80fd\u4fdd\u8bc1\u5217\u8868\u5b9e\u9645\u6307\u5411\u7684\u7c7b\u578b\u662f\u4ec0\u4e48\uff0c\u5e76\u4e0d\u80fd\u4fdd\u8bc1\u5217\u8868\u4e2d\u5b9e\u9645\u5b58\u50a8\u4ec0\u4e48\u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u552f\u4e00\u53ef\u4ee5\u4fdd\u8bc1\u7684\u662f\uff0c\u53ef\u4ee5\u4ece\u4e2d\u8bfb\u53d6\u5230T\u6216\u8005T\u7684\u5b50\u7c7b\u3002\n<\/p>\n<h2>\n\tsuper<br \/>\n<\/h2>\n<p>\n\tList&lt;? super Integer&gt; foo3\u7684\u901a\u914d\u7b26\u58f0\u660e\uff0c\u610f\u5473\u7740\u4ee5\u4e0b\u8d4b\u503c\u662f\u5408\u6cd5\u7684\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\n\/\/ Integer is a &quot;superclass&quot; of Integer (in this context)\r\n\r\nList&lt;? super Integer&gt; foo3 = new ArrayList&lt;Integer&gt;();\r\n\r\n\/\/ Number is a superclass of Integer\r\n\r\nList&lt;? super Integer&gt; foo3 = new ArrayList&lt;Number&gt;();\r\n\r\n\/\/ Object is a superclass of Integer\r\n\r\nList&lt;? super Integer&gt; foo3 = new ArrayList&lt;Object&gt;();<\/pre>\n<p>\n\t\u8bfb\u53d6\u64cd\u4f5c\u4ee5\u4e0a\u7ed9\u5b9a\u7684\u8d4b\u503c\u8bed\u53e5\uff0c\u4e00\u5b9a\u80fd\u4ecefoo3\u5217\u8868\u4e2d\u8bfb\u53d6\u5230\u7684\u5143\u7d20\u662f\u4ec0\u4e48\uff1f\n<\/p>\n<ul>\n<li>\n\t\t\u4e0d\u80fd\u4fdd\u8bc1\u8bfb\u5230Integer\uff0c\u56e0\u4e3afoo3\u53ef\u80fd\u6307\u5411List&lt;Number&gt;\u6216\u8005List&lt;Object&gt;\u3002\n\t<\/li>\n<li>\n\t\t\u4e0d\u80fd\u4fdd\u8bc1\u8bfb\u5230Number\uff0c\u56e0\u4e3afoo3\u53ef\u80fd\u6307\u5411List&lt;Object&gt;\u3002\n\t<\/li>\n<\/ul>\n<p>\n\t\u552f\u4e00\u53ef\u4ee5\u4fdd\u8bc1\u7684\u662f\uff0c\u4f60\u53ef\u4ee5\u8bfb\u53d6\u5230Object\u6216\u8005Object\u5b50\u7c7b\u7684\u5bf9\u8c61\uff0c\u5e76\u4e0d\u77e5\u9053\u5177\u4f53\u7684\u5b50\u7c7b\u662f\u4ec0\u4e48\u3002\n<\/p>\n<p>\n\t\u5199\u5165\u64cd\u4f5c\u901a\u8fc7\u4ee5\u4e0a\u7ed9\u5b9a\u7684\u8d4b\u503c\u8bed\u53e5\uff0c\u80fd\u628a\u4ec0\u4e48\u7c7b\u578b\u7684\u5143\u7d20\u5408\u6cd5\u7684\u63d2\u5165\u5230foo3\u4e2d\uff1f\n<\/p>\n<ul>\n<li>\n\t\t\u80fd\u63d2\u5165Integer\u5bf9\u8c61\uff0c\u56e0\u4e3a\u4e0a\u8ff0\u58f0\u660e\u7684\u5217\u8868\u90fd\u652f\u6301Integer\u3002\n\t<\/li>\n<li>\n\t\t\u80fd\u63d2\u5165Integer\u7684\u5b50\u7c7b\u5bf9\u8c61\uff0c\u56e0\u4e3aInteger\u7684\u5b50\u7c7b\u540c\u65f6\u4e5f\u662fInteger\uff0c\u539f\u56e0\u540c\u4e0a\u3002\n\t<\/li>\n<li>\n\t\t\u4e0d\u80fd\u63d2\u5165Double\u5bf9\u8c61\uff0c\u56e0\u4e3afoo3\u53ef\u80fd\u6307\u5411ArrayList&lt;Integer&gt;\u3002\n\t<\/li>\n<li>\n\t\t\u4e0d\u80fd\u63d2\u5165Number\u5bf9\u8c61\uff0c\u56e0\u4e3afoo3\u53ef\u80fd\u6307\u5411ArrayList&lt;Integer&gt;\u3002\n\t<\/li>\n<li>\n\t\t\u4e0d\u80fd\u63d2\u5165Object\u5bf9\u8c61\uff0c\u56e0\u4e3afoo3\u53ef\u80fd\u6307\u5411ArrayList&lt;Integer&gt;\u3002\n\t<\/li>\n<\/ul>\n<h2>\n\tPECS<br \/>\n<\/h2>\n<p>\n\t\u8bf7\u8bb0\u4f4fPECS\u539f\u5219\uff1a<span style=\"color:#000080\"><strong>\u751f\u4ea7\u8005\uff08Producer\uff09\u4f7f\u7528extends\uff0c\u6d88\u8d39\u8005\uff08Consumer\uff09\u4f7f\u7528super\u3002<\/strong><\/span>\n<\/p>\n<p>\n\t\uff081\uff09<strong>\u751f\u4ea7\u8005\u4f7f\u7528extends<\/strong><br \/>\n\t\u5982\u679c\u9700\u8981\u4e00\u4e2a\u5217\u8868\u63d0\u4f9bT\u7c7b\u578b\u7684\u5143\u7d20\uff08\u5373\u4f60\u60f3\u4ece\u5217\u8868\u4e2d\u8bfb\u53d6T\u7c7b\u578b\u7684\u5143\u7d20\uff09\uff0c\u4f60\u9700\u8981\u628a\u8fd9\u4e2a\u5217\u8868\u58f0\u660e\u6210&lt;? extends T&gt;\uff0c\u6bd4\u5982 List&lt;? extends Integer&gt;\uff0c\u56e0\u6b64\u4f60\u4e0d\u80fd\u5f80\u8be5\u5217\u8868\u4e2d\u6dfb\u52a0\u4efb\u4f55\u5143\u7d20\u3002<br \/>\n\t\uff082\uff09<strong>\u6d88\u8d39\u8005\u4f7f\u7528super<\/strong><br \/>\n\t\u5982\u679c\u9700\u8981\u4e00\u4e2a\u5217\u8868\u4f7f\u7528T\u7c7b\u578b\u7684\u5143\u7d20\uff08\u5373\u4f60\u60f3\u628aT\u7c7b\u578b\u7684\u5143\u7d20\u52a0\u5165\u5230\u5217\u8868\u4e2d\uff09\uff0c\u4f60\u9700\u8981\u628a\u8fd9\u4e2a\u5217\u8868\u58f0\u660e\u6210&lt;? super T&gt;\uff0c\u6bd4\u5982 List&lt;? super Integer&gt;\uff0c\u56e0\u6b64\u4f60\u4e0d\u80fd\u4fdd\u8bc1\u4ece\u4e2d\u8bfb\u53d6\u5230\u7684\u5143\u7d20\u7684\u7c7b\u578b\u3002<br \/>\n\t\uff083\uff09<strong>\u65e2\u662f\u6d88\u8d39\u8005\u4e5f\u662f\u751f\u4ea7\u8005<\/strong><br \/>\n\t\u5982\u679c\u4e00\u4e2a\u5217\u8868\u65e2\u8981\u6d88\u8d39\uff0c\u53c8\u8981\u751f\u4ea7\uff0c\u90a3\u5c31\u4e0d\u80fd\u4f7f\u7528\u6cdb\u578b\u901a\u914d\u7b26\u58f0\u660e\u5217\u8868\uff0c\u8981\u4f7f\u7528\u660e\u786e\u7684\u6cdb\u578b\uff0c\u6bd4\u5982 List&lt;Integer&gt;\u3002\n<\/p>\n<p>\n\t\u4ee5\u4e0b\u662fJDK8\u4e2djava.util.Collections\u4e2dcopy\u7684\u4ee3\u7801\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\npublic static &lt;T&gt; void copy(List&lt;? super T&gt; dest, List&lt;? extends T&gt; src) {\r\n    int srcSize = src.size();\r\n    if (srcSize &gt; dest.size())\r\n        throw new IndexOutOfBoundsException(&quot;Source does not fit in dest&quot;);\r\n\r\n    if (srcSize &lt; COPY_THRESHOLD ||\r\n        (src instanceof RandomAccess &amp;&amp; dest instanceof RandomAccess)) {\r\n        for (int i=0; i&lt;srcSize; i++)\r\n            dest.set(i, src.get(i));\r\n    } else {\r\n        ListIterator&lt;? super T&gt; di=dest.listIterator();\r\n        ListIterator&lt;? extends T&gt; si=src.listIterator();\r\n        for (int i=0; i&lt;srcSize; i++) {\r\n            di.next();\r\n            di.set(si.next());\r\n        }\r\n    }\r\n}<\/pre>\n<p>\n\t\u4ece\u4ee3\u7801\u4e2d\u53ef\u4ee5\u770b\u5230\u4f5c\u4e3a\u751f\u4ea7\u8005\u7684src\u7684\u58f0\u660e\u662fList&lt;? extends T&gt;\uff0c\u4f5c\u4e3a\u6d88\u8d39\u8005\u7684dest\u7684\u58f0\u660e\u662fList&lt;? super T&gt;\u3002\u53ef\u4ee5\u518d\u7528\u4e00\u53e5\u8bdd\u7b80\u5355\u7684\u4fbf\u4e8e\u7406\u89e3\uff0c\u5047\u5982T\u662fNumber\uff0c\u5982\u679c\u751f\u4ea7\u8005\u751f\u4ea7Integer\u7c7b\u578b\uff0c\u90a3\u4e48\u6d88\u8d39\u8005\u6d88\u8d39Object\u7c7b\u578b\uff0c\u660e\u663e\u6d88\u8d39\u8005\u53ef\u4ee5\u6d88\u8d39\uff1b\u5982\u679c\u751f\u4ea7\u8005\u751f\u4ea7Object\u7c7b\u578b\uff0c\u90a3\u4e48\u6d88\u8d39\u8005\u6d88\u8d39Integer\u7c7b\u578b\uff0c\u660e\u786e\u6d88\u8d39\u8005\u4e0d\u53ef\u4ee5\u6d88\u8d39\uff1b\u53ef\u4ee5\u770b\u5230<span style=\"color:#FF0000\"><strong>Object &gt; Number &gt; Integer<\/strong><\/span>\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=1058\">\u6cdb\u578b\u4e2d ? super T \u548c ? extends T \u7684\u533a\u522b<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>&lt;? super T&gt;\u8868\u793a\u5305\u62ecT\u5728\u5185\u7684\u4efb\u4f55T\u7684\u7236\u7c7b\uff0c&lt;? extends T&gt;\u8868\u793a\u5305\u62ec [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[795,793,69,794,792],"class_list":["post-1058","post","type-post","status-publish","format-standard","hentry","category-java","tag-collections","tag-extend","tag-java","tag-super","tag-792"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1058","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1058"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1058\/revisions"}],"predecessor-version":[{"id":1059,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1058\/revisions\/1059"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1058"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1058"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1058"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}