{"id":306,"date":"2023-12-12T15:08:37","date_gmt":"2023-12-12T07:08:37","guid":{"rendered":"http:\/\/ericw.top\/?p=306"},"modified":"2023-12-12T15:09:50","modified_gmt":"2023-12-12T07:09:50","slug":"java%e6%96%b9%e6%b3%95%e5%bc%95%e7%94%a8%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"http:\/\/ericw.top\/?p=306","title":{"rendered":"JAVA\u65b9\u6cd5\u5f15\u7528\u8be6\u89e3"},"content":{"rendered":"<h1 id=\"toc-0\">JAVA\u65b9\u6cd5\u5f15\u7528\u8be6\u89e3<\/h1>\n<p><img decoding=\"async\" src=\"http:\/\/ericw.top\/wp-content\/uploads\/2023\/12\/R-C-300x200.jpg\" alt=\"\" \/><\/p>\n<p>\u5728Java\u4e2d\uff0c\u65b9\u6cd5\u5f15\u7528\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u5b83\u5141\u8bb8\u60a8\u5728<code class=\"prettyprint\" >Lambda\u8868\u8fbe\u5f0f<\/code>\u4e2d\u5f15\u7528\u65b9\u6cd5\uff0c\u800c\u4e0d\u662f\u5728\u8868\u8fbe\u5f0f\u4e2d\u76f4\u63a5\u5b9a\u4e49\u8fd9\u4e9b\u65b9\u6cd5\u3002\u65b9\u6cd5\u5f15\u7528\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u53ef\u8bfb\uff0c\u5c24\u5176\u5728\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<h2 id=\"toc-1\">\u5f15\u7528\u7c7b\u65b9\u6cd5<\/h2>\n<p><strong>\u5f15\u7528\u7c7b\u65b9\u6cd5\u662f\u4e00\u79cd\u6700\u7b80\u5355\u7684\uff0c\u5f15\u7528\u5f0f\u65b9\u6cd5\uff0c\u5b83\u5141\u8bb8\u4f60\u5f15\u7528\u9759\u6001\u65b9\u6cd5\u3002<\/strong><\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<p>\u5f15\u7528\u7c7b\u65b9\u6cd5\u7684\u8bed\u6cd5\u4e3a\uff1a<\/p>\n<pre><code class=\"prettyprint\"  class=\"language-java\">ClassName::staticMethodName<\/code><\/pre>\n<p>\u5176\u4e2d\uff0c<code class=\"prettyprint\" >ClassName<\/code>\u662f\u5305\u542b\u9759\u6001\u65b9\u6cd5\u7684\u7c7b\u7684\u540d\u79f0\uff0c<code class=\"prettyprint\" >staticMethodName<\/code>\u662f\u8981\u5f15\u7528\u7684\u9759\u6001\u65b9\u6cd5\u7684\u540d\u79f0\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u8ba9\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u793a\u4f8b\u6765\u6f14\u793a\u5f15\u7528\u7c7b\u65b9\u6cd5\u7684\u7528\u6cd5\u3002\u5047\u8bbe\u6709\u4e00\u4e2a\u540d\u4e3a<code class=\"prettyprint\" >MathUtils<\/code>\u7684\u5de5\u5177\u7c7b\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a\u9759\u6001\u65b9\u6cd5<code class=\"prettyprint\" >square<\/code>\uff0c\u7528\u4e8e\u8ba1\u7b97\u4e00\u4e2a\u6570\u7684\u5e73\u65b9\u3002<\/p>\n<pre><code class=\"prettyprint\"  class=\"language-java\">\/\/ MathUtils.java\npublic class MathUtils {\n    public static int square(int num) {\n        return num * num;\n    }\n}<\/code><\/pre>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u65b9\u6cd5\u5f15\u7528\u6765\u5f15\u7528\u8fd9\u4e2a\u9759\u6001\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"prettyprint\"  class=\"language-java\">import java.util.function.IntUnaryOperator;\n\npublic class MethodReferenceExample {\n    public static void main(String[] args) {\n        IntUnaryOperator squareFunction = MathUtils::square;\n\n        int result = squareFunction.applyAsInt(5);\n        System.out.println(&quot;Square of 5 is &quot; + result);\n    }\n}<\/code><\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a<code class=\"prettyprint\" >IntUnaryOperator<\/code>\u51fd\u6570\u5f0f\u63a5\u53e3\u7684\u5b9e\u4f8b<code class=\"prettyprint\" >squareFunction<\/code>\uff0c\u5e76\u5c06<code class=\"prettyprint\" >MathUtils::square<\/code>\u4f5c\u4e3a\u5f15\u7528\u4f20\u9012\u7ed9\u5b83\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code class=\"prettyprint\" >applyAsInt<\/code>\u65b9\u6cd5\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u5e76\u4f20\u5165\u53c2\u65705\uff0c\u5f97\u5230\u8ba1\u7b97\u7ed3\u679c\u3002<\/p>\n<p>\u8fd9\u91cc\uff0c<code class=\"prettyprint\" >MathUtils::square<\/code>\u5f15\u7528\u4e86<code class=\"prettyprint\" >MathUtils<\/code>\u7c7b\u7684\u9759\u6001\u65b9\u6cd5<code class=\"prettyprint\" >square<\/code>\uff0c\u5e76\u4e14\u53ef\u4ee5\u50cfLambda\u8868\u8fbe\u5f0f\u4e00\u6837\u4f7f\u7528\u3002<\/p>\n<h2 id=\"toc-2\">\u5f15\u7528\u5bf9\u8c61\u7684\u5b9e\u4f8b\u65b9\u6cd5<\/h2>\n<p><strong>\u5f15\u7528\u5bf9\u8c61\u7684\u5b9e\u4f8b\u65b9\u6cd5\u662f\u4e00\u79cd\u66f4\u5e38\u89c1\u548c\u6709\u7528\u7684\u65b9\u6cd5\u5f15\u7528\u65b9\u5f0f\u3002<\/strong>\u5b83\u5141\u8bb8\u60a8\u5f15\u7528\u5df2\u7ecf\u5b58\u5728\u7684\u5bf9\u8c61\u7684\u5b9e\u4f8b\u65b9\u6cd5\uff0c\u800c\u4e0d\u662f\u5728Lambda\u8868\u8fbe\u5f0f\u4e2d\u5b9a\u4e49\u65b0\u7684\u65b9\u6cd5\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<p>\u5f15\u7528\u5bf9\u8c61\u7684\u5b9e\u4f8b\u65b9\u6cd5\u7684\u8bed\u6cd5\u4e3a\uff1a<\/p>\n<pre><code class=\"prettyprint\"  class=\"language-java\">object::instanceMethodName<\/code><\/pre>\n<p>\u5176\u4e2d\uff0c<code class=\"prettyprint\" >object<\/code>\u662f\u8981\u5f15\u7528\u65b9\u6cd5\u7684\u5bf9\u8c61\u7684\u540d\u79f0\u6216\u5f15\u7528\uff0c<code class=\"prettyprint\" >instanceMethodName<\/code>\u662f\u8981\u5f15\u7528\u7684\u5b9e\u4f8b\u65b9\u6cd5\u7684\u540d\u79f0\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u8ba9\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u793a\u4f8b\u6765\u6f14\u793a\u5f15\u7528\u5bf9\u8c61\u7684\u5b9e\u4f8b\u65b9\u6cd5\u7684\u7528\u6cd5\u3002\u5047\u8bbe\u6709\u4e00\u4e2a\u540d\u4e3a<code class=\"prettyprint\" >Person<\/code>\u7684\u7c7b\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a\u5b9e\u4f8b\u65b9\u6cd5<code class=\"prettyprint\" >printName<\/code>\uff0c\u7528\u4e8e\u6253\u5370\u4eba\u7684\u59d3\u540d\u3002<\/p>\n<pre><code class=\"prettyprint\"  class=\"language-java\">public class Person {\n    private String name;\n\n    public Person(String name) {\n        this.name = name;\n    }\n\n    public void printName() {\n        System.out.println(&quot;Name: &quot; + name);\n    }\n}<\/code><\/pre>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u65b9\u6cd5\u5f15\u7528\u6765\u5f15\u7528<code class=\"prettyprint\" >printName<\/code>\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"prettyprint\"  class=\"language-java\">import java.util.function.Consumer;\n\npublic class MethodReferenceExample {\n    public static void main(String[] args) {\n        Person person = new Person(&quot;Alice&quot;);\n\n        Consumer&lt;Person&gt; printNameFunction = Person::printName;\n        printNameFunction.accept(person);\n    }\n}<\/code><\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a<code class=\"prettyprint\" >Consumer&lt;Person&gt;<\/code>\u51fd\u6570\u5f0f\u63a5\u53e3\u7684\u5b9e\u4f8b<code class=\"prettyprint\" >printNameFunction<\/code>\uff0c\u5e76\u5c06<code class=\"prettyprint\" >Person::printName<\/code>\u4f5c\u4e3a\u5f15\u7528\u4f20\u9012\u7ed9\u5b83\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code class=\"prettyprint\" >accept<\/code>\u65b9\u6cd5\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u5e76\u4f20\u5165\u4e00\u4e2a<code class=\"prettyprint\" >Person<\/code>\u5bf9\u8c61\uff0c\u5b83\u4f1a\u6253\u5370\u8be5\u5bf9\u8c61\u7684\u59d3\u540d\u3002<\/p>\n<p>\u8fd9\u91cc\uff0c<code class=\"prettyprint\" >Person::printName<\/code>\u5f15\u7528\u4e86<code class=\"prettyprint\" >Person<\/code>\u7c7b\u7684\u5b9e\u4f8b\u65b9\u6cd5<code class=\"prettyprint\" >printName<\/code>\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u51fd\u6570\u5f0f\u63a5\u53e3\u4e2d\u4f7f\u7528\u3002<\/p>\n<h2 id=\"toc-3\">\u5f15\u7528\u7c7b\u7684\u5b9e\u4f8b\u65b9\u6cd5<\/h2>\n<p>\u5f15\u7528\u7c7b\u7684\u5b9e\u4f8b\u65b9\u6cd5\u7684\u8bed\u6cd5\u4e3a\uff1a<\/p>\n<pre><code class=\"prettyprint\"  class=\"language-java\">ClassName::instanceMethodName<\/code><\/pre>\n<p>\u5176\u4e2d\uff0c<code class=\"prettyprint\" >ClassName<\/code>\u662f\u5305\u542b\u5b9e\u4f8b\u65b9\u6cd5\u7c7b\u7684\u540d\u79f0\uff0c<code class=\"prettyprint\" >instanceMethodName<\/code>\u662f\u8981\u5f15\u7528\u7684\u5b9e\u4f8b\u65b9\u6cd5\u7684\u540d\u79f0<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u8ba9\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u793a\u4f8b\u6765\u6f14\u793a\u5f15\u7528\u7c7b\u7684\u5b9e\u4f8b\u65b9\u6cd5\u7684\u7528\u6cd5\u3002\u5047\u8bbe\u6709\u4e00\u4e2a\u540d\u4e3a<code class=\"prettyprint\" >Person<\/code>\u7684\u7c7b\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a\u5b9e\u4f8b\u65b9\u6cd5<code class=\"prettyprint\" >getName<\/code>\uff0c\u7528\u4e8e\u83b7\u53d6\u4eba\u7684\u59d3\u540d\u3002<\/p>\n<pre><code class=\"prettyprint\"  class=\"language-java\">public class Person {\n    private String name;\n\n    public Person(String name) {\n        this.name = name;\n    }\n\n    public String getName() {\n        return name;\n    }\n}<\/code><\/pre>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u65b9\u6cd5\u5f15\u7528\u6765\u5f15\u7528<code class=\"prettyprint\" >getName<\/code>\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"prettyprint\"  class=\"language-java\">import java.util.List;\nimport java.util.function.Function;\n\npublic class MethodReferenceExample {\n    public static void main(String[] args) {\n        List&lt;Person&gt; people = List.of(new Person(&quot;Alice&quot;), new Person(&quot;Bob&quot;));\n\n        Function&lt;Person, String&gt; getNameFunction = Person::getName;\n\n        for (Person person : people) {\n            String name = getNameFunction.apply(person);\n            System.out.println(&quot;Name: &quot; + name);\n        }\n    }\n}<\/code><\/pre>\n<h2 id=\"toc-4\">\u5f15\u7528\u6784\u9020\u5668<\/h2>\n<p>\u5f15\u7528\u6784\u9020\u5668\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u65b9\u6cd5\u5f15\u7528\u65b9\u5f0f\uff0c\u7528\u4e8e\u521b\u5efa\u5bf9\u8c61\u3002\u5b83\u5141\u8bb8\u60a8\u5f15\u7528\u7c7b\u7684\u6784\u9020\u5668\uff0c\u4ee5\u4fbf\u5728Lambda\u8868\u8fbe\u5f0f\u4e2d\u521b\u5efa\u5bf9\u8c61\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<p>\u5f15\u7528\u6784\u9020\u5668\u7684\u8bed\u6cd5\u4e3a\uff1a<\/p>\n<pre><code class=\"prettyprint\"  class=\"language-java\">ClassName::new<\/code><\/pre>\n<p>\u5176\u4e2d\uff0c<code class=\"prettyprint\" >ClassName<\/code>\u662f\u8981\u5f15\u7528\u6784\u9020\u5668\u7684\u7c7b\u7684\u540d\u79f0<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u8ba9\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u793a\u4f8b\u6765\u6f14\u793a\u5f15\u7528\u6784\u9020\u5668\u7684\u7528\u6cd5\u3002\u5047\u8bbe\u6709\u4e00\u4e2a\u540d\u4e3a<code class=\"prettyprint\" >Person<\/code>\u7684\u7c7b\uff0c\u5b83\u6709\u4e00\u4e2a\u5e26\u53c2\u6570\u7684\u6784\u9020\u5668\u3002<\/p>\n<pre><code class=\"prettyprint\"  class=\"language-java\">public class Person {\n    private String name;\n\n    public Person(String name) {\n        this.name = name;\n    }\n\n    public String getName() {\n        return name;\n    }\n}<\/code><\/pre>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u65b9\u6cd5\u5f15\u7528\u6765\u5f15\u7528<code class=\"prettyprint\" >Person<\/code>\u7c7b\u7684\u6784\u9020\u5668\uff1a<\/p>\n<pre><code class=\"prettyprint\"  class=\"language-java\">import java.util.function.Supplier;\n\npublic class MethodReferenceExample {\n    public static void main(String[] args) {\n        Supplier&lt;Person&gt; personSupplier = Person::new;\n\n        Person person = personSupplier.get();\n        System.out.println(&quot;Created person with name: &quot; + person.getName());\n    }\n}<\/code><\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a<code class=\"prettyprint\" >Supplier&lt;Person&gt;<\/code>\u51fd\u6570\u5f0f\u63a5\u53e3\u7684\u5b9e\u4f8b<code class=\"prettyprint\" >personSupplier<\/code>\uff0c\u5e76\u5c06<code class=\"prettyprint\" >Person::new<\/code>\u4f5c\u4e3a\u5f15\u7528\u4f20\u9012\u7ed9\u5b83\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code class=\"prettyprint\" >get<\/code>\u65b9\u6cd5\u8c03\u7528<code class=\"prettyprint\" >personSupplier<\/code>\u6765\u521b\u5efa\u4e00\u4e2a`Person\u5bf9\u8c61\u3002<\/p>\n<p>\u8fd9\u91cc\uff0c<code class=\"prettyprint\" >Person::new<\/code>\u5f15\u7528\u4e86<code class=\"prettyprint\" >Person<\/code>\u7c7b\u7684\u6784\u9020\u5668\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u51fd\u6570\u5f0f\u63a5\u53e3\u4e2d\u4f7f\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JAVA\u65b9\u6cd5\u5f15\u7528\u8be6\u89e3 \u5728Java\u4e2d\uff0c\u65b9\u6cd5\u5f15\u7528\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u5b83\u5141\u8bb8\u60a8\u5728Lambda\u8868\u8fbe\u5f0f\u4e2d\u5f15\u7528\u65b9\u6cd5\uff0c\u800c\u4e0d\u662f\u5728 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":310,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,5],"tags":[],"_links":{"self":[{"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/posts\/306"}],"collection":[{"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ericw.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=306"}],"version-history":[{"count":2,"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/posts\/306\/revisions"}],"predecessor-version":[{"id":311,"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/posts\/306\/revisions\/311"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/media\/310"}],"wp:attachment":[{"href":"http:\/\/ericw.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ericw.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=306"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ericw.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}