{"id":499,"date":"2024-11-07T11:02:36","date_gmt":"2024-11-07T03:02:36","guid":{"rendered":"https:\/\/eve2333.top\/?p=499"},"modified":"2024-11-08T14:49:35","modified_gmt":"2024-11-08T06:49:35","slug":"cs61b%e5%ad%a6%e4%b9%a0-part1","status":"publish","type":"post","link":"https:\/\/eve2333.top\/?p=499","title":{"rendered":"CS61b\u5b66\u4e60-part1"},"content":{"rendered":"\n<p>\u672c\u4eba\u9009\u62e9\u4e862018spring\u7684\u8bfe\u7a0b\uff0c\u56e0\u4e3a\u4ed6\u514d\u8d39\u63d0\u4f9b\u4e86\u8bc4\u5206\u673a\u5668\uff0c\u540e\u6765\u5f97\u77e52021\u4e5f\u5f00\u653e\u4e86\uff0c\u51b3\u5b9a\u628a\u5176\u4e2d\u7684Lab\u5c1d\u8bd5\u4e00\u756a\uff0c\u542c\u8bf4gitlab\u5c31\u8fd1\u597d\u8bc4\uff0c\u76f8\u5f53\u6709\u5b9e\u529b\uff0c\u5e76\u501f\u6b64\u5b66\u4e60Java\u7684\u57fa\u672c\u77e5\u8bc6\uff0c\u8bf7\u6839\u636epku\u7684cswiki\u505a\u597d\u8bc4\u5206\u673a\u5668\u51c6\u5907\uff0c\u8bf7\u81ea\u884c\u4e0b\u8f7dIJ IDEA\uff0c\u53ef\u4ee5\u9009\u62e9\u7834\u89e3\u4e13\u4e1a\u7248\uff0c\u611f\u8c22\u4f2f\u514b\u5229\u5927\u5b66\u548cJosh Hug\u5f00\u6e90\u5982\u6b64\u4f18\u8d28\u77e5\u8bc6<\/p>\n\n\n\n<p>\u6839\u636e\u524d\u4eba\u6240\u8bf4\uff0c\u6b64\u8bfe\u7a0b\u9700\u8981200h\uff0c\u6211\u7b2c\u4e00\u4e2a\u540c\u5b66\u82b1\u8d39\u4e86\u7ea630\u523040\u5929\u6691\u5047\u65f6\u95f4\u5b66\u5b8c\u4e86\uff0c\u4f30\u7b97\u5927\u6982\u4e00\u59295-7h\u7684\u4e13\u7cbe\u5b66\u4e60\u65f6\u95f4\uff0c\u73b0\u5728\u6211\u5728\u5b66\u6821\uff0c\u5e0c\u671bfrom9.18\u5927\u698250day\u53ef\u4ee5\u5b8c\u6210\ud83d\ude2d\ud83d\ude2d\ud83d\ude2d\ud83d\ude2d\ud83d\ude2d\u5931\u8d25\u4e8611.7\u4e5f\u6ca1\u770b\u5b8c\uff0c\u90fd\u6028\u90a3\u4e2asb\u7684\u4eba\u5de5\u667a\u80fd\u76f8\u5173\u5185\u5bb9<\/p>\n\n\n\n<p>\u7b2c\u4e00\u4e8cparts\u7b80\u8ff0\u4e86\u5b66\u671f\u4efb\u52a1\uff0c\u8bc4\u4f30\u76ee\u6807\u4e0e\u8981\u6c42\uff0c\u6d4b\u8bc4\u65b9\u6cd5\u7b49\u7b49\u3002\u770b\u770b\u5c31\u597d.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Java\u7684\u57fa\u7840\u77e5\u8bc6<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>public class HelloWorld{\n    public static void main(String&#91;] args){\n        System.out.println(\"hello world\");\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>public class HelloWorld{\n    public static void main(String&#91;] args){\n        int x=0;\n        while(x&lt;10){\n            System.put.println(x);\n            x=x+1;\n        }\n        x=\"horse\";\n    }\n}<\/code><\/pre>\n\n\n\n<p>1. Before Java variables can be used, they must be declared.<br>2. Java variables must have a specific type.<br>3. Java variable types can never change.<br>4. Types are verified before the code even runs!!!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class LargerDemo{\n\/** Returns the Larger of x and y.*\/\n    public static int larger(int x, int y) {\n        if (x &gt; y) {\n            return x;\n        }\n        return y;\n    }\n    public static void main(String&#91;] args){\n        System.out.println(larger(-5, 10));\n    }\n}\n\/*\n1. Functions must be declared as part of a class in Java. A function that is part of a cLass is calLed a \"method\". So in Java, all functions are methods.\n2. To define a function in Java, we use \"public static\". We will see alternate ways of defining functions Later. \n3. ALL parameters of a function must have a decLared type,\nand the return value of the function must have a decLared type. Functions in Java return onLy one value!\n*\/\n<\/code><\/pre>\n\n\n\n<p>why\u5728jdk23\u4e2d\u8981\u6c42\u7f16\u8bd1\u4ee5\u4e0a\u6587\u4ef6 C:\\Users\\eve\\IdeaProjects\\newprojects\\src\\Main.java<br>java: \u7c7b LargerDemo \u662f\u516c\u5171\u7684, \u5e94\u5728\u540d\u4e3a LargerDemo.java \u7684\u6587\u4ef6\u4e2d\u58f0\u660e\uff0cOK\u6211\u77e5\u9053\u4e86\uff0c\u53ea\u8981\u628a\u6587\u4ef6\u540d\u4fee\u6539\u4e00\u4e0b\u5373\u53ef \u200b<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"887\" height=\"484\" src=\"https:\/\/eve2333.top\/wp-content\/uploads\/2024\/11\/1730948298-\u5c4f\u5e55\u622a\u56fe-2024-11-07-105804.png\" alt=\"\" class=\"wp-image-500\" style=\"width:700px;height:auto\" srcset=\"https:\/\/eve2333.top\/wp-content\/uploads\/2024\/11\/1730948298-\u5c4f\u5e55\u622a\u56fe-2024-11-07-105804.png 887w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/11\/1730948298-\u5c4f\u5e55\u622a\u56fe-2024-11-07-105804-300x164.png 300w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/11\/1730948298-\u5c4f\u5e55\u622a\u56fe-2024-11-07-105804-768x419.png 768w\" sizes=\"auto, (max-width: 887px) 100vw, 887px\" \/><\/figure>\n\n\n\n<p>lab1\u548chomework0\u90fd\u5f88\u7b80\u5355\u3002\u662f\u5bf9Java\u7684\u7b80\u5355\u5e94\u7528\u3002\u53ef\u80fdcmd\u90e8\u5206\u5f88\u6709\u7528\uff0c\u4e0d\u8fc7\u4f60\u5e94\u8be5\u5728linux\u90e8\u5206\u8fdb\u884c\u4e86\u8be6\u7ec6\u5b66\u4e60\uff0cgit\u4f60\u4e5f\u662f\u5b66\u4e60\u4f7f\u7528\u8fc7\u4e86<\/p>\n\n\n\n<p>\u53d1\u73b0joshhug.gitbooks.io\/hug61b\u662f\u8bfe\u7a0b\u6559\u6848\u4e00\u6837\u7684\u4e1c\u897f.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Creating and Defining Classes<\/h1>\n\n\n\n<p>\u6211\u4eec\u53ea\u5728cmd\u8f93\u5165ls\u8fdb\u5165\u5230HelloWorld.java\u6587\u4ef6\u4e2d\uff0cjavac HelloWorld.java\u5373\u53ef\u7f16\u8bd1\u8fd0\u884c\uff0c\u4f46\u662f\u5f97\u5230\u4e86class\u6587\u4ef6\uff0c\u8fd9\u65f6\u6211\u4eecJava HelloWorld\u5c31\u53ef\u4ee5\u89e3\u91ca\u8fd0\u884c\u6210\u529f\uff0c\u8f93\u51fahelloworld<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"578\" src=\"https:\/\/eve2333.top\/wp-content\/uploads\/2024\/11\/1730948327-\u5c4f\u5e55\u622a\u56fe-2024-11-07-105827-1024x578.png\" alt=\"\" class=\"wp-image-501\" style=\"width:608px;height:auto\" srcset=\"https:\/\/eve2333.top\/wp-content\/uploads\/2024\/11\/1730948327-\u5c4f\u5e55\u622a\u56fe-2024-11-07-105827-1024x578.png 1024w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/11\/1730948327-\u5c4f\u5e55\u622a\u56fe-2024-11-07-105827-300x169.png 300w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/11\/1730948327-\u5c4f\u5e55\u622a\u56fe-2024-11-07-105827-768x433.png 768w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/11\/1730948327-\u5c4f\u5e55\u622a\u56fe-2024-11-07-105827.png 1042w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Dog{\n    public static void makeNoise(){\n        System.out.println(\"Bark!\");\n       }\n}\npublic class DogLauncher{\n    public static void main(String&#91;] args){\n         Dog. makeNoise();\n    }    \n}<\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u5b9e\u4ece\u7c7b\u4e2d\u4f8b\u4e00\u4e2a\u5bf9\u8c61<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Dog{\n    public int weightInPounds;\n\n    public \/*static*\/ void makeNoise(){     \n        if (weightInPounds &lt;10){\n             System.out. println(\"yip!\");\n        } else if (weightInPounds &lt; 30){ \n            System.out. println(\"bark.\");\n        } else{\n            System.out. println(\"woooof!\");\n        }\n    } \n} \n<\/code><\/pre>\n\n\n\n<p>\u8fd0\u884c\u540e\u6211\u4eec\u6309\u7167\u7f16\u8bd1\u5668\u4fe1\u606f\u5220\u9664static\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2ad\u7684dog.\u91cd\u590d\uff0c\u6267\u884c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class DogLaimcher{\n    public static void main (String&#91;] args)\n        Dog d =new Dog();\n        d.weightInPounds=51;\n        d.makeNoise();\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u6784\u9020\u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public Dog(int w){\n    wewightInPounds=w;\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6837\u53ef\u4ee5Dog d=new Dog\uff0851\uff09\uff1bd.makeNoise\uff08\uff09\uff1b\u6210\u529f\u8fd0\u884c<\/p>\n\n\n\n<p>\u73b0\u5728\u9610\u8ff0\u4e3a\u4ec0\u4e48\u53bb\u6389static\uff0c\u56e0\u4e3a\u4f7f\u7528\u4e86\u5176\u4e2d\u7684\u5b9e\u4f8b\u53d8\u91cf\uff0c\u4e0estatic\u51b2\u7a81\uff0c\u8fd9\u4f7f\u7f16\u8bd1\u5668\u611f\u5230\u56f0\u60d1 \u200b<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"591\" src=\"https:\/\/eve2333.top\/wp-content\/uploads\/2024\/11\/1730948474-\u5c4f\u5e55\u622a\u56fe-2024-11-07-110058.png\" alt=\"\" class=\"wp-image-502\" style=\"width:579px;height:auto\" srcset=\"https:\/\/eve2333.top\/wp-content\/uploads\/2024\/11\/1730948474-\u5c4f\u5e55\u622a\u56fe-2024-11-07-110058.png 1024w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/11\/1730948474-\u5c4f\u5e55\u622a\u56fe-2024-11-07-110058-300x173.png 300w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/11\/1730948474-\u5c4f\u5e55\u622a\u56fe-2024-11-07-110058-768x443.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><strong>\u00a0\u7f3a\u5c11\u5bf9\u4e0b\u9762\u6b64\u56fe\u7247\u90e8\u5206\u7b14\u8bb0<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"963\" height=\"1024\" src=\"https:\/\/eve2333.top\/wp-content\/uploads\/2024\/11\/1730948505-\u5c4f\u5e55\u622a\u56fe-2024-11-07-110130-963x1024.png\" alt=\"\" class=\"wp-image-503\" style=\"width:591px;height:auto\" srcset=\"https:\/\/eve2333.top\/wp-content\/uploads\/2024\/11\/1730948505-\u5c4f\u5e55\u622a\u56fe-2024-11-07-110130-963x1024.png 963w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/11\/1730948505-\u5c4f\u5e55\u622a\u56fe-2024-11-07-110130-282x300.png 282w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/11\/1730948505-\u5c4f\u5e55\u622a\u56fe-2024-11-07-110130-768x817.png 768w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/11\/1730948505-\u5c4f\u5e55\u622a\u56fe-2024-11-07-110130.png 1034w\" sizes=\"auto, (max-width: 963px) 100vw, 963px\" \/><\/figure>\n\n\n\n<p>&nbsp;\u4f60\u53ef\u4ee5\u62e5\u6709\u4e00\u4e2a\u9759\u6001\u548c\u975e\u9759\u6001\u7684\u7c7b,\u628a\u4ed6\u4eec\u6df7\u5408<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Dog{\npublic int weightInPounds;\npublic static String binomen=\"Canis familiaris\"\n\/*\u521b\u5efa\u4e00\u4e2a\u9002\u7528\u4e8e\u6240\u6709\u72d7\u7684\u53d8\u91cf\uff0c\u53ef\u4ee5\u5728\u5bf9\u8c61d\uff0cd2\u6216\u8005Dog\u4f7f\u7528*\/\n\npublic Dog(int w){\n    weightInPounds=w;\n}\npublic void makeNoise(){\n    if (weightInPounds 10){ \n        System.out. println(\"yip!\"); \n    }else if (weightInPounds 30){ \n        System.out. println(\"bark.\"); \n    }else{\n        System.out. println(\"woooof! \"); \n    }\n}\npublic static Dog maxDog(Dog d1, Dog d2){ \n    if (dl. weightInPounds d2.weightInPounds) {\n        return d1;\n    }\n    return d2;\n}\n\npublic Dog maxDog(Dog d2){\n    \/*\u975e\u9759\u6001\u65b9\u6cd5\uff0c\u56e0\u4e3a\u662f\u7531\u7279\u5b9a\u7684dog\u8fdb\u884c\u5224\u65ad*\/\n    if (this.weightInPounds&gt;d2.weightInPound){\n     return this;\n    }\n    return d2;\n}\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u4e00\u4e2a\u7c7b\u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\u6216\u65b9\u6cd5\u4e5f\u79f0\u4e3a\u8be5\u7c7b\u7684\u6210\u5458.<br>\u4f7f\u7528\u7c7b\u540d\u8bbf\u95ee\u9759\u6001\u6210\u5458,\u4f8b\u5982Dog.binomen.<br>\u4e0d\u80fd\u4f7f\u7528\u7c7b\u540d\u8c03\u7528\u975e\u9759\u6001\u6210\u5458:<s>Dog.makeNoise<\/s><br>\u9759\u6001\u65b9\u6cd5\u5fc5\u987b\u901a\u8fc7\u7279\u5b9a\u7684\u5b9e\u4f8b\u8bbf\u95ee\u5b9e\u65f6\u53d8\u91cf,\u4f8b\u5982d1.<\/p>\n\n\n\n<p>\u6bd4\u5982\u6211\u4eec\u5220\u9664\u8fd9\u4e2a\u65b9\u6cd5Dog maxDog\uff08Dog d2\uff09\u7f16\u8bd1\u5668\u62a5\u9519\u3002\u5bf9\u4e8e\u975e\u9759\u6001\u6210\u5458\uff0c\u5982\u679c\u53ea\u6709\u4e00\u4e2a\u65b9\u6cd5\u7684\u975e\u9759\u6001\u7248\uff0c\u5c31\u4e0d\u80fd\u7528\u7c7b\u540d\u6765\u8fd0\u884c\u51fd\u6570\uff1b\u5982\u679c\u6709\u4e00\u4e2a\u9759\u6001\u65b9\u6cd5\uff0c\u5e76\u4e14\u60f3\u8bbf\u95ee\u67d0\u79cd\u5b9e\u4f8b\u53d8\u91cf\uff0c\u5fc5\u987b\u6307\u660e\u662f\u54ea\u4e2a\u5b9e\u4f8b\uff08\u6bd4\u5982\u91cc\u9762\u7684this.\uff09<\/p>\n\n\n\n<p><strong>\u56de\u7b54\u4ee5\u4e0b\u4ee3\u7801\u4f1a\u8f93\u51fa\u4ec0\u4e48\uff1f<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class DogLoop {\n    public static void main(String&#91;] args) {\n        Dog smallDog = new Dog(5);\n        Dog mediumDog = new Dog(25);\n        Dog hugeDog = new Dog(150);\n\n        Dog&#91;] manyDogs = new Dog&#91;4];\n        manyDogs&#91;0] = smallDog;\n        manyDogs&#91;1] = hugeDog;\n        manyDogs&#91;2] = new Dog(130);\n\n        int i = 0;\n        while (i &lt; manyDogs.length) {\n            Dog.maxDog(manyDogs&#91;i], mediumDog).makeNoise();\n            i = i + 1;\n        }\n    }\npublic static class Dog {\n      \/* Size of the dog in standard dog size units. *\/\n      public int size;\n\n      \/* This is a constructor. It tells us how to construct\n\t * dogs from our ideal notion of dogness. *\/\n\n      public Dog(int s) {\n         size = s;\n      }\n\n      public void makeNoise() {\n         if (size &lt; 10) {\n            System.out.println(\"hideous yapping\");\n         } else if (size &lt; 30) {\n            System.out.println(\"bark!\");\n         } else {\n            System.out.println(\"woof!\");\n         }\n      }\n\n      \/* Return the larger of dog d1 and dog d2. *\/\n      public static Dog maxDog(Dog d1, Dog d2) {\n         if (d1.size &gt; d2.size) {\n            return d1;\n         }\n         return d2;\n      }   \n   }\n}\n<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/cscircles.cemc.uwaterloo.ca\/java_visualize\/#\" target=\"_blank\"  rel=\"nofollow\" >\u8fd9\u662f\u4e00\u4e2ajava\u53ef\u89c6\u5316\u7f51\u7ad9&nbsp; &nbsp; Java Visualizer (uwaterloo.ca)<\/a>\u5bf9\u4ee5\u4e0a\u95ee\u9898\u505a\u51fa\u56de\u7b54\uff01<\/p>\n\n\n\n<p>\u4e00\u70b9\u6269\u5c55\u5728creating and defining classes9\/10\u4e2d0.0-2.20\u7684\u8bdd<\/p>\n\n\n\n<p><strong>\u73b0\u5728\u8981\u6c42\u521b\u5efa\u4e00\u4e2a\u7a0b\u5e8fArgsSum\uff0c\u6253\u5370\u51fa\u547d\u4ee4\u53c2\u6570\u7684\u603b\u548c\uff0c\u5047\u8bbe\u4ed6\u4eec\u662f\u6570\u5b57<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class ArgsSum {\n    public static void main(String&#91;] args) {\n        int N = args.length;\n        int i = 0;\n        int sum = 0;\n        while (i &lt; N) {\n            sum = sum + Integer.parseInt(args&#91;i]);\/*string to int\u7684\u65b9\u6cd5*\/\n            i = i + 1;\n        }\n        System.out.println(sum);\n    }\n}<\/code><\/pre>\n\n\n\n<p>Libraries\u5e93\u7684\u4f7f\u7528 \u63a8\u8350\u4f7f\u7528stackoverflow<\/p>\n\n\n\n<p>\u666e\u6797\u65af\u987f\u5927\u5b66library\uff0c<strong>\u8bf7\u505aproject0\uff1f<\/strong> \u200b<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u4eba\u9009\u62e9\u4e862018spring\u7684\u8bfe\u7a0b\uff0c\u56e0\u4e3a\u4ed6\u514d\u8d39\u63d0\u4f9b\u4e86\u8bc4\u5206\u673a\u5668\uff0c\u540e\u6765\u5f97\u77e52021\u4e5f\u5f00\u653e\u4e86\uff0c\u51b3\u5b9a\u628a\u5176\u4e2d\u7684Lab\u5c1d\u8bd5\u4e00\u756a\uff0c\u542c\u8bf4gitl &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[2],"tags":[17,18],"class_list":["post-499","post","type-post","status-publish","format-standard","hentry","category-2","tag-17","tag-18"],"_links":{"self":[{"href":"https:\/\/eve2333.top\/index.php?rest_route=\/wp\/v2\/posts\/499","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eve2333.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eve2333.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eve2333.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/eve2333.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=499"}],"version-history":[{"count":0,"href":"https:\/\/eve2333.top\/index.php?rest_route=\/wp\/v2\/posts\/499\/revisions"}],"wp:attachment":[{"href":"https:\/\/eve2333.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=499"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eve2333.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=499"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eve2333.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}