{"id":503,"date":"2023-05-27T19:19:52","date_gmt":"2023-05-27T11:19:52","guid":{"rendered":"https:\/\/fugary.com\/?p=503"},"modified":"2023-05-27T19:19:52","modified_gmt":"2023-05-27T11:19:52","slug":"chatgpt%e6%80%bb%e7%bb%93%e7%9a%84java%e8%af%ad%e8%a8%80%e6%9c%80%e6%9c%89%e7%89%b9%e8%89%b2%e7%9a%84%e5%87%a0%e4%b8%aa%e7%89%b9%e6%80%a7","status":"publish","type":"post","link":"https:\/\/fugary.com\/?p=503","title":{"rendered":"ChatGPT\u603b\u7ed3\u7684Java\u8bed\u8a00\u6700\u6709\u7279\u8272\u7684\u51e0\u4e2a\u7279\u6027"},"content":{"rendered":"<p>\u4ee5\u4e0b\u662fJava\u8bed\u8a00\u6700\u6709\u7279\u8272\u7684\u51e0\u4e2a\u7279\u6027\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u6309\u7279\u6027\u5206\u7ec4\u5c55\u793a\uff1a<\/p>\n<h4>\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b (OOP)<\/h4>\n<p>Java\u662f\u4e00\u79cd\u9762\u5411\u5bf9\u8c61\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u652f\u6301\u5c01\u88c5\u3001\u7ee7\u627f\u548c\u591a\u6001\u7b49\u9762\u5411\u5bf9\u8c61\u7684\u7279\u6027\u3002<\/p>\n<pre><code class=\"language-java\">\/\/ \u5c01\u88c5\nclass Car {\n    private String brand;\n    private int price;\n\n    public void setBrand(String brand) {\n        this.brand = brand;\n    }\n\n    public void setPrice(int price) {\n        this.price = price;\n    }\n\n    public void displayInfo() {\n        System.out.println(&quot;Brand: &quot; + brand);\n        System.out.println(&quot;Price: &quot; + price);\n    }\n}\n\n\/\/ \u7ee7\u627f\nclass ElectricCar extends Car {\n    private int batteryCapacity;\n\n    public void setBatteryCapacity(int batteryCapacity) {\n        this.batteryCapacity = batteryCapacity;\n    }\n\n    public void displayInfo() {\n        super.displayInfo();\n        System.out.println(&quot;Battery Capacity: &quot; + batteryCapacity);\n    }\n}\n\n\/\/ \u591a\u6001\npublic class Main {\n    public static void main(String[] args) {\n        Car car1 = new Car();\n        car1.setBrand(&quot;Toyota&quot;);\n        car1.setPrice(20000);\n        car1.displayInfo();\n\n        ElectricCar car2 = new ElectricCar();\n        car2.setBrand(&quot;Tesla&quot;);\n        car2.setPrice(50000);\n        car2.setBatteryCapacity(75);\n        car2.displayInfo();\n    }\n}<\/code><\/pre>\n<h4>\u5f02\u5e38\u5904\u7406 (Exception Handling)<\/h4>\n<p>Java\u901a\u8fc7\u5f02\u5e38\u5904\u7406\u673a\u5236\u6765\u5904\u7406\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u60c5\u51b5\u3002<\/p>\n<pre><code class=\"language-java\">import java.io.File;\nimport java.io.FileNotFoundException;\nimport java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        try {\n            File file = new File(&quot;input.txt&quot;);\n            Scanner scanner = new Scanner(file);\n            while (scanner.hasNextLine()) {\n                String line = scanner.nextLine();\n                System.out.println(line);\n            }\n            scanner.close();\n        } catch (FileNotFoundException e) {\n            System.out.println(&quot;File not found.&quot;);\n        }\n    }\n}<\/code><\/pre>\n<h4>\u591a\u7ebf\u7a0b (Multithreading)<\/h4>\n<p>Java\u63d0\u4f9b\u4e86\u591a\u7ebf\u7a0b\u7f16\u7a0b\u7684\u652f\u6301\uff0c\u53ef\u4ee5\u540c\u65f6\u6267\u884c\u591a\u4e2a\u7ebf\u7a0b\u6765\u5b9e\u73b0\u5e76\u53d1\u64cd\u4f5c\u3002<\/p>\n<pre><code class=\"language-java\">class MyThread extends Thread {\n    private String name;\n\n    public MyThread(String name) {\n        this.name = name;\n    }\n\n    public void run() {\n        for (int i = 0; i &lt; 5; i++) {\n            System.out.println(name + &quot;: &quot; + i);\n            try {\n                Thread.sleep(500);\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            }\n        }\n    }\n}\n\npublic class Main {\n    public static void main(String[] args) {\n        MyThread thread1 = new MyThread(&quot;Thread 1&quot;);\n        MyThread thread2 = new MyThread(&quot;Thread 2&quot;);\n\n        thread1.start();\n        thread2.start();\n    }\n}<\/code><\/pre>\n<h4>\u6cdb\u578b (Generics)<\/h4>\n<p>Java\u7684\u6cdb\u578b\u673a\u5236\u5141\u8bb8\u5728\u7f16\u8bd1\u65f6\u6307\u5b9a\u7c7b\u578b\u53c2\u6570\uff0c\u589e\u52a0\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\u548c\u7c7b\u578b\u5b89\u5168\u6027\u3002<\/p>\n<pre><code class=\"language-java\">import java.util.ArrayList;\nimport java.util.List;\n\npublic class Main {\n    public static void main(String[] args) {\n        List&lt;String&gt; names = new ArrayList&lt;&gt;();\n        names.add(&quot;Alice&quot;);\n        names.add(&quot;Bob&quot;);\n        names.add(&quot;Charlie&quot;);\n\n        for (String name : names) {\n            System.out.println(name);\n        }\n    }\n}<\/code><\/pre>\n<p>\u5f53\u7136\uff01\u4ee5\u4e0b\u662f\u66f4\u591aJava\u8bed\u8a00\u6700\u6709\u7279\u8272\u7684\u7f16\u7801\u7279\u6027\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u6309\u7279\u6027\u5206\u7ec4\u5c55\u793a\uff1a<\/p>\n<h4>Lambda\u8868\u8fbe\u5f0f<\/h4>\n<p>Java 8\u5f15\u5165\u4e86Lambda\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u7b80\u6d01\u5730\u7f16\u5199\u533f\u540d\u51fd\u6570\uff0c\u5e76\u7528\u4e8e\u51fd\u6570\u5f0f\u7f16\u7a0b\u548c\u96c6\u5408\u64cd\u4f5c\u3002<\/p>\n<pre><code class=\"language-java\">import java.util.Arrays;\nimport java.util.List;\n\npublic class Main {\n    public static void main(String[] args) {\n        List&lt;String&gt; names = Arrays.asList(&quot;Alice&quot;, &quot;Bob&quot;, &quot;Charlie&quot;);\n\n        names.forEach(name -&gt; System.out.println(name));\n\n        List&lt;Integer&gt; numbers = Arrays.asList(1, 2, 3, 4, 5);\n        int sum = numbers.stream()\n                         .filter(n -&gt; n % 2 == 0)\n                         .mapToInt(n -&gt; n)\n                         .sum();\n\n        System.out.println(&quot;Sum of even numbers: &quot; + sum);\n    }\n}<\/code><\/pre>\n<h4>Stream API<\/h4>\n<p>Java 8\u5f15\u5165\u4e86Stream API\uff0c\u63d0\u4f9b\u4e86\u4e00\u79cd\u6d41\u5f0f\u64cd\u4f5c\u96c6\u5408\u7684\u65b9\u5f0f\uff0c\u652f\u6301\u51fd\u6570\u5f0f\u7f16\u7a0b\u548c\u5e76\u884c\u5904\u7406\u3002<\/p>\n<pre><code class=\"language-java\">import java.util.Arrays;\nimport java.util.List;\n\npublic class Main {\n    public static void main(String[] args) {\n        List&lt;String&gt; names = Arrays.asList(&quot;Alice&quot;, &quot;Bob&quot;, &quot;Charlie&quot;);\n\n        long count = names.stream()\n                          .filter(name -&gt; name.length() &gt; 4)\n                          .count();\n\n        System.out.println(&quot;Count of names with more than 4 characters: &quot; + count);\n    }\n}<\/code><\/pre>\n<h4>Optional\u7c7b<\/h4>\n<p>Java 8\u5f15\u5165\u4e86Optional\u7c7b\uff0c\u7528\u4e8e\u5904\u7406\u53ef\u80fd\u4e3a\u7a7a\u7684\u503c\uff0c\u907f\u514d\u4e86\u7a7a\u6307\u9488\u5f02\u5e38\u3002<\/p>\n<pre><code class=\"language-java\">import java.util.Optional;\n\npublic class Main {\n    public static void main(String[] args) {\n        Optional&lt;String&gt; name = Optional.ofNullable(getName());\n\n        if (name.isPresent()) {\n            System.out.println(&quot;Name: &quot; + name.get());\n        } else {\n            System.out.println(&quot;Name not found&quot;);\n        }\n    }\n\n    public static String getName() {\n        return &quot;Alice&quot;;\n    }\n}<\/code><\/pre>\n<h4>\u6ce8\u89e3 (Annotations)<\/h4>\n<p>Java\u652f\u6301\u4f7f\u7528\u6ce8\u89e3\uff08Annotations\uff09\u6765\u4e3a\u7a0b\u5e8f\u5143\u7d20\u6dfb\u52a0\u5143\u6570\u636e\uff0c\u5e76\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u7684\u6ce8\u89e3\uff0c\u5982@Override\u548c@Deprecated\u7b49\u3002<\/p>\n<pre><code class=\"language-java\">import java.util.ArrayList;\nimport java.util.List;\n\n@SuppressWarnings(&quot;unused&quot;)\npublic class Main {\n    @Deprecated\n    public static void oldMethod() {\n        System.out.println(&quot;This method is deprecated.&quot;);\n    }\n\n    public static void main(String[] args) {\n        List&lt;String&gt; names = new ArrayList&lt;&gt;();\n\n        names.add(&quot;Alice&quot;);\n        names.add(&quot;Bob&quot;);\n\n        \/\/ \u4f7f\u7528\u4e86@SuppressWarnings\u6ce8\u89e3\uff0c\u5ffd\u7565\u7f16\u8bd1\u5668\u7684\u8b66\u544a\n        names.forEach(System.out::println);\n\n        \/\/ \u4f7f\u7528\u4e86@Deprecated\u6ce8\u89e3\uff0c\u8868\u793a\u8be5\u65b9\u6cd5\u5df2\u7ecf\u8fc7\u65f6\n        oldMethod();\n    }\n}<\/code><\/pre>\n<p>\u5f53\u7136\uff01\u4ee5\u4e0b\u662f\u66f4\u591aJava\u8bed\u8a00\u6700\u6709\u7279\u8272\u7684\u7f16\u7801\u7279\u6027\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u6309\u7279\u6027\u5206\u7ec4\u5c55\u793a\uff1a<\/p>\n<h4>\u53cd\u5c04 (Reflection)<\/h4>\n<p>Java\u7684\u53cd\u5c04\u673a\u5236\u5141\u8bb8\u5728\u8fd0\u884c\u65f6\u68c0\u67e5\u548c\u64cd\u4f5c\u7c7b\u3001\u5bf9\u8c61\u548c\u65b9\u6cd5\u7b49\u4fe1\u606f\u3002<\/p>\n<pre><code class=\"language-java\">import java.lang.reflect.Constructor;\nimport java.lang.reflect.Field;\nimport java.lang.reflect.Method;\n\npublic class Main {\n    public static void main(String[] args) throws Exception {\n        \/\/ \u83b7\u53d6\u7c7b\u7684\u4fe1\u606f\n        Class&lt;?&gt; clazz = Class.forName(&quot;com.example.Person&quot;);\n        System.out.println(&quot;Class Name: &quot; + clazz.getName());\n\n        \/\/ \u83b7\u53d6\u6784\u9020\u51fd\u6570\n        Constructor&lt;?&gt; constructor = clazz.getConstructor(String.class, int.class);\n        Object person = constructor.newInstance(&quot;Alice&quot;, 25);\n\n        \/\/ \u83b7\u53d6\u5b57\u6bb5\n        Field nameField = clazz.getDeclaredField(&quot;name&quot;);\n        nameField.setAccessible(true);\n        System.out.println(&quot;Name: &quot; + nameField.get(person));\n\n        \/\/ \u8c03\u7528\u65b9\u6cd5\n        Method helloMethod = clazz.getDeclaredMethod(&quot;hello&quot;);\n        helloMethod.invoke(person);\n    }\n}\n\nclass Person {\n    private String name;\n    private int age;\n\n    public Person(String name, int age) {\n        this.name = name;\n        this.age = age;\n    }\n\n    private void hello() {\n        System.out.println(&quot;Hello, &quot; + name);\n    }\n}<\/code><\/pre>\n<h4>\u5e8f\u5217\u5316 (Serialization)<\/h4>\n<p>Java\u7684\u5e8f\u5217\u5316\u673a\u5236\u5141\u8bb8\u5c06\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u8282\u6d41\u8fdb\u884c\u5b58\u50a8\u6216\u4f20\u8f93\uff0c\u5e76\u5728\u9700\u8981\u65f6\u91cd\u65b0\u6784\u9020\u5bf9\u8c61\u3002<\/p>\n<pre><code class=\"language-java\">import java.io.*;\n\npublic class Main {\n    public static void main(String[] args) throws IOException, ClassNotFoundException {\n        \/\/ \u5bf9\u8c61\u5e8f\u5217\u5316\n        Person person = new Person(&quot;Alice&quot;, 25);\n        FileOutputStream fileOut = new FileOutputStream(&quot;person.ser&quot;);\n        ObjectOutputStream out = new ObjectOutputStream(fileOut);\n        out.writeObject(person);\n        out.close();\n        fileOut.close();\n\n        \/\/ \u5bf9\u8c61\u53cd\u5e8f\u5217\u5316\n        FileInputStream fileIn = new FileInputStream(&quot;person.ser&quot;);\n        ObjectInputStream in = new ObjectInputStream(fileIn);\n        Person deserializedPerson = (Person) in.readObject();\n        in.close();\n        fileIn.close();\n\n        System.out.println(&quot;Name: &quot; + deserializedPerson.getName());\n        System.out.println(&quot;Age: &quot; + deserializedPerson.getAge());\n    }\n}\n\nclass Person implements Serializable {\n    private String name;\n    private int age;\n\n    public Person(String name, int age) {\n        this.name = name;\n        this.age = age;\n    }\n\n    public String getName() {\n        return name;\n    }\n\n    public int getAge() {\n        return age;\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u4e0b\u662fJava\u8bed\u8a00\u6700\u6709\u7279\u8272\u7684\u51e0\u4e2a\u7279\u6027\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u6309\u7279\u6027\u5206\u7ec4\u5c55\u793a\uff1a \u9762\u5411\u5bf9\u8c61\u7f16\u7a0b (OOP) Java\u662f\u4e00\u79cd\u9762\u5411 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[107,1],"tags":[119],"_links":{"self":[{"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts\/503"}],"collection":[{"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=503"}],"version-history":[{"count":1,"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts\/503\/revisions"}],"predecessor-version":[{"id":504,"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts\/503\/revisions\/504"}],"wp:attachment":[{"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}