{"id":247,"date":"2024-07-08T12:16:04","date_gmt":"2024-07-08T04:16:04","guid":{"rendered":"https:\/\/forelink.top\/?p=247"},"modified":"2024-07-08T12:16:05","modified_gmt":"2024-07-08T04:16:05","slug":"java%ef%bc%88%e6%b3%9b%e5%9e%8b%ef%bc%89","status":"publish","type":"post","link":"https:\/\/forelink.top\/index.php\/2024\/07\/08\/java%ef%bc%88%e6%b3%9b%e5%9e%8b%ef%bc%89\/","title":{"rendered":"Java\uff08\u6cdb\u578b\uff09"},"content":{"rendered":"\n<p>\u5b9a\u4e49\u7c7b\uff0c\u63a5\u53e3\uff0c\u65b9\u6cd5\u65f6\uff0c\u53ef\u4ee5\u7528&lt;&gt;\u5728\u7f16\u8bd1\u9636\u6bb5\u786e\u5b9a\u591a\u79cd\u6570\u636e\u7c7b\u578b\u3002<\/p>\n\n\n\n<p>\u5206\u4e3a\u6cdb\u578b\u7c7b\u3001\u6cdb\u578b\u63a5\u53e3\u3001\u6cdb\u578b\u65b9\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u6ca1\u6709\u4f7f\u7528\u6cdb\u578b \u53ef\u4ee5\u6dfb\u52a0\u4e00\u5207\u7c7b\u578b\u6570\u636e\nArrayList list1 = new ArrayList();\n\/\/\u4f7f\u7528\u4e86\u6cdb\u578b \u53ea\u80fd\u63a5\u53d7String\u7c7b\u578b\u7684\u6570\u636e\n\/\/ArrayList&lt;String&gt; list2= new ArrayList&lt;String&gt;();\nArrayList&lt;String&gt; list2= new ArrayList&lt;&gt;();\/\/jdk 1.7\u540e\u540e\u9762\u53ef\u4ee5\u4e0d\u58f0\u660e<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading has-medium-font-size\">\u6cdb\u578b\u7684\u539f\u7406<\/h2>\n\n\n\n<p>\u5728\u58f0\u660e\u6cdb\u578b\u540e\uff0c\u5c06&lt;\u7c7b\u578b&gt;\u4f20\u7ed9\u4e86&lt;E&gt;\uff0c\u8ba9\u8fd9\u4e2a\u53ea\u80fd\u63a5\u53d7\u548c\u8fd4\u56de\u5bf9\u5e94\u7c7b\u578b\u7684\u6570\u636e\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-medium-font-size\">\u6cdb\u578b\u7c7b<\/h2>\n\n\n\n<p>\u683c\u5f0f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u7c7b\u578b\u53d8\u91cf\u53ef\u4ee5\u662f\u4efb\u4f55\u5b57\u6bcd \u4e3b\u8981\u662f\u8868\u8fbe\u4e00\u4e2a\u53ef\u53d8\u53c2\u6570\n\u4fee\u9970\u7b26 class \u7c7b\u540d &lt;\u7c7b\u578b\u53d8\u91cf, \u7c7b\u578b\u53d8\u91cf&gt;{\n    \n}<\/code><\/pre>\n\n\n\n<p>\u793a\u4f8b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class MyArrayList&lt;E&gt; {\n    private Object&#91;] arr = new Object&#91;10];\n    private int size;\n    public boolean add(E e){\n        arr&#91;size++] = e;\n        return true;\n    }\n    public E get(int index){\n        \/\/alt + enter \u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\n        return (E) arr&#91;index];\n    }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading has-medium-font-size\">\u6cdb\u578b\u63a5\u53e3<\/h2>\n\n\n\n<p>\uff08\u4fdd\u5b58\u6570\u636e\u548c\u6839\u636e\u7d22\u5f15\u67e5\u8be2\u6570\u636e\u7528\uff09<\/p>\n\n\n\n<p>public interface Container&lt;E>{<br>    public void add(E item); \/\/ \u6dfb\u52a0\u4e00\u4e2aE<br>    E get(int index); \/\/ \u6839\u636e\u7d22\u5f15\u83b7\u5f97\u67d0\u4e2aE<br>    E remove(int index);\/\/ \u6839\u636e\u7d22\u5f15\u5220\u9664\u67d0\u4e2aE<br>}<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-medium-font-size\">\u6cdb\u578b\u65b9\u6cd5<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u6cdb\u578b\u65b9\u6cd5\u53ef\u4ee5\u5728\u65b9\u6cd5\u8fd4\u56de\u7c7b\u578b\u4e4b\u524d\u58f0\u660e\u7c7b\u578b\u53c2\u6570\u3002\npublic class lister {\n    public static void main(String&#91;] args) {\n        String&#91;] Stringlist={\"1\",\"b\",\"e\"};\n        Integer&#91;] intlist={1,2,3};\n        printArray(Stringlist);\n        printArray(intlist);\n    }\n    public static &lt;E> void printArray(E&#91;] list){\n        for(E e : list){\n            System.out.print(e+\" \");\n        }\n        System.out.println();\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528\u6cdb\u578b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5efa\u7acb\u6570\u636e\u901a\u7528\u7684\u7f13\u5b58\u6c60\uff0c\u5e76\u5bf9\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u8fdb\u884c\u5904\u7406\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-medium-font-size\">\u901a\u914d\u7b26<\/h2>\n\n\n\n<p>\u901a\u914d\u7b26\u7528\u4e8e\u5904\u7406\u7c7b\u578b\u4e0d\u786e\u5b9a\u7684\u60c5\u51b5\uff0c\u5982\u5904\u7406\u5177\u6709\u4e0d\u540c\u7c7b\u578b\u53c2\u6570\u7684\u96c6\u5408\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void printList(List&lt;?> list) {\n    for (Object obj : list) {\n        System.out.println(obj);\n    }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading has-medium-font-size\">\u9650\u5236\u7c7b\u578b\u53c2\u6570<\/h2>\n\n\n\n<p>\u901a\u8fc7\u4f7f\u7528 \uff1f\u3001extend \u548c super \u5173\u952e\u5b57\uff0c\u53ef\u4ee5\u9650\u5236\u6cdb\u578b\u7c7b\u578b\u53c2\u6570\u7684\u8303\u56f4\uff0c\u4ee5\u589e\u5f3a\u6cdb\u578b\u5b89\u5168\u6027<\/p>\n\n\n\n<p>\u901a\u914d\u7b26\u4e3b\u8981\u6709\u4e09\u79cd\u7c7b\u578b\uff1a<br>\u65e0\u9650\u5236\u901a\u914d\u7b26\uff08<code>?<\/code>\uff09<br>\u4e0a\u754c\u901a\u914d\u7b26\uff08A extends B\uff09<br>    \u4fdd\u8bc1\u83b7\u53d6\u7684\u5bf9\u8c61\u662fB\u6216\u8005\u662fB\u7684\u5b50\u7c7b\uff0c\u9002\u7528\u4e8e\u8bfb\u53d6\u6570\u636e\u7684\u573a\u666f\uff0c\u4fdd\u8bc1\u81f3\u5c11\u662fB\u7c7b\u578b<br>     \u5982\uff1a\u63a5\u53d7Number\u7c7b\u578b\u6216\u5176\u5b50\u7c7b<br>\u4e0b\u754c\u901a\u914d\u7b26\uff08A super B\uff09<br>    \u4fdd\u8bc1\u83b7\u53d6\u7684\u5bf9\u8c61\u662fB\u6216\u8005\u662fB\u7684\u7236\u7c7b\uff0c\u9002\u7528\u4e8e\u5199\u5165\u6570\u636e\u7684\u573a\u666f\uff0c\u4fdd\u8bc1\u81f3\u5c11\u662fB\u7c7b\u578b<br>     \u5982\uff1a\u5199\u5165Interger\u7c7b\u578b\u6216\u5176\u7236\u7c7b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u4e0a\u754c\u901a\u914d\u7b26\npublic void printNumber(List&lt;? extends Number> list){\n    \/\/ \u53ea\u63a5\u53d7\u5b58\u5165Number\u7c7b\u578b\u53ca\u5176\u5b50\u7c7b\u7684list\u96c6\u5408\u3002\n}\n\n\/\/ \u4e0b\u754c\u901a\u914d\u7b26\npublic void addNumber(List&lt;? super Integer> list){\n    \/\/ \u53ea\u63a5\u53d7\u5199\u5165Integer\u7c7b\u578b\u53ca\u5176\u7236\u7c7b\u7684list\u96c6\u5408\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading has-medium-font-size\">\u603b\u7ed3<\/h2>\n\n\n\n<p>\u6cdb\u578b\u7684\u597d\u5904\uff1a<br>   <strong>\u7c7b\u578b\u5b89\u5168<\/strong>\uff1a\u5728\u7f16\u8bd1\u65f6\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5\uff0c\u51cf\u5c11\u8fd0\u884c\u65f6\u7c7b\u578b\u9519\u8bef\u3002<br>   <strong>\u6d88\u9664\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362<\/strong>\uff1a\u4e0d\u9700\u8981\u663e\u5f0f\u7684\u7c7b\u578b\u8f6c\u6362\uff0c\u63d0\u9ad8\u4ee3\u7801\u53ef\u8bfb\u6027\u3002<br>   <strong>\u4ee3\u7801\u91cd\u7528\u6027<\/strong>\uff1a\u901a\u8fc7\u6cdb\u578b\uff0c\u53ef\u4ee5\u7f16\u5199\u4e0e\u7c7b\u578b\u65e0\u5173\u7684\u901a\u7528\u4ee3\u7801\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b9a\u4e49\u7c7b\uff0c\u63a5\u53e3\uff0c\u65b9\u6cd5\u65f6\uff0c\u53ef\u4ee5\u7528&lt;&gt;\u5728\u7f16\u8bd1\u9636\u6bb5\u786e\u5b9a\u591a\u79cd\u6570\u636e\u7c7b\u578b\u3002 \u5206\u4e3a\u6cdb\u578b\u7c7b\u3001\u6cdb\u578b\u63a5\u53e3\u3001\u6cdb\u578b\u65b9\u6cd5 \u6cdb\u578b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[21,1],"tags":[],"class_list":["post-247","post","type-post","status-publish","format-standard","hentry","category-java","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/forelink.top\/index.php\/wp-json\/wp\/v2\/posts\/247","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/forelink.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/forelink.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/forelink.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/forelink.top\/index.php\/wp-json\/wp\/v2\/comments?post=247"}],"version-history":[{"count":2,"href":"https:\/\/forelink.top\/index.php\/wp-json\/wp\/v2\/posts\/247\/revisions"}],"predecessor-version":[{"id":286,"href":"https:\/\/forelink.top\/index.php\/wp-json\/wp\/v2\/posts\/247\/revisions\/286"}],"wp:attachment":[{"href":"https:\/\/forelink.top\/index.php\/wp-json\/wp\/v2\/media?parent=247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/forelink.top\/index.php\/wp-json\/wp\/v2\/categories?post=247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/forelink.top\/index.php\/wp-json\/wp\/v2\/tags?post=247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}