【原】Java學習筆記008 - 方法(函數)。本站提示廣大學習愛好者:(【原】Java學習筆記008 - 方法(函數))文章只能為提供參考,不一定能成為您想要的結果。以下是【原】Java學習筆記008 - 方法(函數)正文
1 package cn.temptation;
2
3 public class Sample01 {
4 public static void main(String[] args) {
5 // 方法/函數 Method/Function
6
7 // 為什麼會出現方法?
8 // 原因1、在程序中多處出現相同的語句內容
9 // 原因2、會發生變化
10
11 // 【只有變化是不變的】
12 // 回顧一下變量,為了應對變化,提出的新的機制
13 // 需要對變化進行封裝處理,在這裡就是要對一系列的語句進行封裝,得到方法的概念
14 // 【方法的封裝性】
15
16 // 方法的格式:
17 // 訪問修飾符 返回類型 方法名(參數類型1 參數名1, 參數類型2 參數名2, ..., 參數類型n 參數名n) {
18 // 方法體內各種執行語句;
19 // return 返回值; // return語句用於有返回類型的場合
20 // }
21
22 // 1、訪問修飾符:public static(公開的靜態的)
23 // 2、返回類型:① 無返回的 void;② 有返回的
24 // 3、方法名:方法的名稱,在方法被調用時使用
25 // 4、方法名後的括號裡可以有參數列表(有參),也可以沒有參數列表(無參)
26 // 5、參數列表:參數類型1 參數名1, 參數類型2 參數名2, ..., 參數類型n 參數名n
27
28 // 【方法的使用原則:不調用,不執行】
29
30 // 方法的調用形式:方法名(參數列表);
31
32 // 做輸出的事情
33 // System.out.println("天氣不太好");
34 //// System.out.println("網絡不太好");
35 // System.out.println("網絡現在太好");
36 // 把對語句的使用,替換為對方法的調用
37 show();
38
39 // 做了一些其他的事情
40 // ...
41
42 // 做輸出的事情
43 // System.out.println("天氣不太好");
44 //// System.out.println("網絡不太好");
45 // System.out.println("網絡現在太好");
46 // 把對語句的使用,替換為對方法的調用
47 show();
48
49 // 做了一些其他的事情
50 // ...
51
52 // 做輸出的事情
53 // System.out.println("天氣不太好");
54 //// System.out.println("網絡不太好");
55 // System.out.println("網絡現在太好");
56 // 把對語句的使用,替換為對方法的調用
57 show();
58 }
59
60 // 在和主函數main同級的位置,制作方法
61 public static void show() {
62 // 做輸出的事情
63 System.out.println("天氣不太好");
64 // 當語句發生變化時,使用方法後,不需要對方法的調用發生修改,只需要修改一下方法中的語句即可
65 // System.out.println("網絡不太好");
66 System.out.println("網絡現在太好");
67
68 // void無返回的方法中,寫上return語句也可以,但是return無任何類型的返回值
69 // return;
70 // 語法錯誤:Void methods cannot return a value,不能返回一個具體數據類型的結果
71 // return true;
72 }
73 }
1 package cn.temptation;
2
3 public class Sample02 {
4 public static void main(String[] args) {
5 // 1、方法在某一個類的內部的
6 // 2、在稱呼上,方法 和 函數 其實是一個意思
7 // 3、做事情時,不要強求在一個方法內全部做完,不同的方法做不同的事情
8
9 // 方法對內封裝語句,體現了它的【封裝性】
10 // 方法對外到處被調用,體現了它的【復用性】
11 }
12
13 /**
14 * 方法名:顯示方法的名稱
15 * 方法用途:用於顯示方法的用途
16 * 方法返回值:無
17 * 方法參數名:無
18 * 方法創建人:張三
19 * 方法創建時間:YYYY.MM.DD
20 * 方法修改人:李四
21 * 方法修改時間:YYYY.MM.DD
22 * 方法修改內容:.....
23 */
24 public static void show() {
25
26 }
27 }
28
29 // 語法錯誤:Syntax error on token "}", delete this token
30 // 語法錯誤:Syntax error, insert "}" to complete ClassBody
31 //public static void showOther() {
32 //
33 //}
1 package cn.temptation;
2
3 public class Sample03 {
4 public static void main(String[] args) {
5 // 注意:
6 // 1、方法是不能嵌套方法的,方法和方法之間是平級的
7 // 2、方法名及參數列表均相同的方法是不能被定義的
8
9 // 語法錯誤:Illegal modifier for parameter show; only final is permitted
10 // public static void show() {
11 //
12 // }
13 }
14
15 public static void show() {
16
17 }
18
19 // 語法錯誤:Duplicate method show() in type
20 // public static void show() {
21 //
22 // }
23 }
1 package cn.temptation;
2
3 public class Sample04 {
4 public static void main(String[] args) {
5 // 調用無返回值的方法
6 // add();
7
8 // 調用有返回值的方法
9 // 注意:返回值是什麼數據類型的值,在調用並接收的地方就要聲明什麼數據類型的變量來進行接收
10 // 定義局部變量result,在方法中定義的局部變量只能在該方法中使用,超出該方法無法訪問。
11 // 所以主函數中定義的局部變量 和 同級方法中定義的局部變量,雖然同名同類型,但是互相不受影響
12 int result = add();
13 // 打印出結果
14 System.out.println(result);
15 }
16
17 // 需求:制作一個無返回值的方法,計算兩個數的和
18 // public static void add() {
19 // int i = 2;
20 // int j = 3;
21 //
22 // int result = i + j;
23 //
24 // // 直接打印出結果
25 // System.out.println(result);
26 // }
27
28 // 需求:制作一個有返回值的方法,計算兩個數的和(考慮到計算歸計算,打印歸打印)
29 public static int add() {
30 int i = 2;
31 int j = 3;
32
33 // 定義局部變量result,在方法中定義的局部變量只能在該方法中使用,超出該方法無法訪問
34 int result = i + j;
35
36 // 如果沒有寫return語句,會產生語法錯誤:This method must return a result of type int
37 return result;
38 }
39 }
1 package cn.temptation;
2
3 public class Sample05 {
4 public static void main(String[] args) {
5 // 有參方法的調用
6
7 // 方法的參數:根據方法是被定義還是被調用,將方法的參數分為兩種
8 // 1、形參:形式參數,用於方法定義時,起站位表明參數類型的作用
9 // 2、實參:實際參數,用於方法調用時,不用加上參數類型
10
11 // 注意:形參和實參的數據類型要一致
12
13 int result = add(2, 3);
14 // 實參不需要加上數據類型,加上會有語法錯誤
15 // int result = add(int 2, int 3);
16 System.out.println(result);
17 }
18
19 // 定義方法
20 public static int add(int i, int j) {
21 // 方法內部就可以使用形參來做加法的操作
22 int result = i + j;
23
24 return result;
25 }
26 }
1 package cn.temptation;
2
3 public class Sample06 {
4 public static void main(String[] args) {
5 // 使用方法時需要注意的問題:
6 // 1、方法的定義
7 // ① 方法的返回值類型(輸出)
8 // ② 方法的參數列表(輸入)
9 // 2、方法的調用
10
11 // 方法的調用形式
12 // 1、直接調用:一般用於無返回值的方法
13 // 2、賦值調用:在調用方法的地方,聲明一個和方法返回值類型相同的變量,用以接收方法的返回值(一般用於有返回值的方法)
14 // 3、輸出調用:本質上就是拿著方法的返回值作為輸出語句的方法的實參使用
15
16 // 直接調用
17 test1();
18
19 // 賦值調用
20 int result = test2();
21 System.out.println(result);
22
23 // 輸出調用
24 System.out.println(test2());
25 }
26
27 public static void test1() {
28 System.out.println("直接調用的方法");
29 }
30
31 public static int test2() {
32 System.out.println("有返回值的方法");
33 return 123;
34 }
35 }
1 package cn.temptation;
2
3 import java.util.Scanner;
4
5 public class Sample07 {
6 public static void main(String[] args) {
7 // 需求:制作方法,接收用戶鍵盤錄入,判斷輸入的兩個數字哪個大?
8
9 // 制作方法時,關注點在方法的返回值類型 和 參數列表上
10 // 好的方法都不是一蹴而就的,是不斷提煉出來的
11
12 Scanner input = new Scanner(System.in);
13 System.out.println("輸入一個數字:");
14 int i = input.nextInt();
15 System.out.println("再輸入一個數字:");
16 int j = input.nextInt();
17 input.close();
18
19 // 調用方法,傳遞實參
20 int result = max(i, j);
21 System.out.println("兩個數:" + i + "和" + j + "中較大的數為:" + result);
22 }
23
24 // 根據輸入的數進行比較
25 public static int max(int i, int j) {
26 // 定義一個變量
27 int max = 0;
28
29 if (i > j) {
30 max = i;
31 } else {
32 max = j;
33 }
34
35 return max;
36 }
37 }
1 package cn.temptation;
2
3 import java.util.Scanner;
4
5 public class Sample08 {
6 public static void main(String[] args) {
7 // 需求:制作一個方法,判斷輸入的兩個數是否相等
8 Scanner input = new Scanner(System.in);
9 System.out.println("輸入一個數字:");
10 int i = input.nextInt();
11 System.out.println("再輸入一個數字:");
12 int j = input.nextInt();
13 input.close();
14
15 // 調用方法
16 if (compare(i, j)) {
17 System.out.println("兩數相等");
18 } else {
19 System.out.println("兩數不等");
20 }
21 }
22
23 // 制作方法,比較兩個數是否相同
24 public static boolean compare(int i, int j) {
25 // 定義一個是否相同的標識
26 // boolean flag = true;
27
28 // 寫法1
29 // if (i - j == 0) {
30 // flag = true;
31 // } else {
32 // flag = false;
33 // }
34
35 // 寫法2
36 // if (i == j) {
37 // flag = true;
38 // } else {
39 // flag = false;
40 // }
41
42 // 寫法3
43 // if (i - j != 0) {
44 // flag = false;
45 // }
46
47 // 寫法4
48 // if (i != j) {
49 // flag = false;
50 // }
51 //
52 // return flag;
53
54 // 寫法5
55 // flag = (i == j) ? true : false;
56 // return flag;
57
58 // 寫法6
59 return i == j;
60 }
61 }
1 package cn.temptation;
2
3 import java.util.Scanner;
4
5 public class Sample09 {
6 public static void main(String[] args) {
7 // 需求:制作方法,獲取三個數中最大的一個
8 Scanner input = new Scanner(System.in);
9 System.out.println("輸入第一個數字:");
10 int i = input.nextInt();
11 System.out.println("輸入第二個數字:");
12 int j = input.nextInt();
13 System.out.println("輸入第三個數字:");
14 int k = input.nextInt();
15 input.close();
16
17 // 調用方法
18 int result = max(i, j, k);
19 System.out.println("最大的數為:" + result);
20 }
21
22 // 比較三個數中最大的一個
23 public static int max(int i, int j, int k) {
24 // 定義一個變量max
25 int max = 0;
26
27 // 寫法1
28 // if (i > j) {
29 // if (i > k) {
30 // max = i;
31 // } else {
32 // max = k;
33 // }
34 // } else {
35 // if (j > k) {
36 // max = j;
37 // } else {
38 // max = k;
39 // }
40 // }
41
42 // 寫法2
43 int tempMax = (i > j) ? i : j;
44 max = (tempMax > k) ? tempMax : k;
45
46 return max;
47 }
48 }
1 package cn.temptation;
2
3 public class Sample10 {
4 public static void main(String[] args) {
5 // 方法的重載(英文:Overload)
6 // 定義:方法名相同、參數列表不同的一系列方法稱為方法的重載
7 System.out.println(add(2, 3));
8 System.out.println(add(4, 5, 6));
9 }
10
11 // 需求:制作一個方法,操作兩個數相加
12 public static int add1(int i, int j) {
13 int result = i + j;
14 return result;
15 }
16
17 // 需求:制作一個方法,操作三個數相加
18 public static int add2(int i, int j, int k) {
19 int result = i + j + k;
20 return result;
21 }
22
23 // 需求:制作一個方法,操作四個數相加...
24
25 // 既然這些方法做的都是數字相加的事情,考慮使用相同的方法名add
26
27 // 需求:制作一個方法,操作兩個數相加
28 public static int add(int i, int j) {
29 int result = i + j;
30 return result;
31 }
32
33 // 需求:制作一個方法,操作三個數相加
34 public static int add(int i, int j, int k) {
35 int result = i + j + k;
36 return result;
37 }
38 }
1 package cn.temptation;
2
3 public class Sample11 {
4 public static void main(String[] args) {
5 // 方法重載的注意點:
6 // 1、方法名需要相同
7 // 2、方法的參數列表必須不同,包括參數的類型或個數,以此區分不同的方法
8 // ① 如果參數個數不同,不用考慮參數類型
9 // ②如果參數個數相同,需要考慮參數的類型 或 參數的順序必須不同
10 // 3、方法的返回值類型、訪問修飾符可以相同,也可以不相同
11 }
12
13 public static void show(int i, int j) {
14
15 }
16
17 public static void show(int i, double j) {
18
19 }
20
21 // 參數個數相同,參數類型相同,但是參數名不同的方法不是重載方法
22 // 語法錯誤:Duplicate method show(int, int) in type
23 // public static void show(int a, int b) {
24 //
25 // }
26
27 public static void show(int i, int j, int k) {
28
29 }
30
31 // 和第二個方法參數個數相同、類型相同,但是順序不同,也是重載方法
32 public static void show(double j, int i) {
33
34 }
35
36 // 和第一個方法參數列表相同,但是無法區分參數順序是否不同,所以產生語法錯誤
37 // 語法錯誤:Duplicate method show(int, int) in type
38 // public static void show(int j, int i) {
39 //
40 // }
41
42 // 雖然返回值類型和其他重載方法不同,但是並不影響是重載方法
43 public static boolean show(int i) {
44 return true;
45 }
46
47 // 和第一個方法參數列表相同,但是返回值類型不同,這也無法區分,有語法錯誤
48 // 語法錯誤:Duplicate method show(int, int) in type
49 // public static boolean show(int i, int j) {
50 // return false;
51 // }
52 }
1 package cn.temptation;
2
3 public class Sample12 {
4 public static void main(String[] args) {
5 // 方法調用時,匹配參數的數據類型,優先選擇參數類型完全匹配的重載方法
6 System.out.println(test(3, 4));
7 // System.out.println(test(3.4, 4.5));
8
9 // 注釋掉重載方法1、5,保留重載方法2、3、4時有語法錯誤
10 // The method test(double, double) is ambiguous for the type
11
12 // 注釋掉重載方法1、2、5,保留重載方法3、4時有語法錯誤
13 // The method test(int, double) is ambiguous for the type
14
15 // 注釋掉重載方法1、2、3、4,保留重載方法5時有語法錯誤
16 // The method test() in the type Sample12 is not applicable for the arguments (int, int)
17
18 // 調用方法重載時,參數匹配規則:
19 // 1、如果有參數類型完全匹配的重載方法,會自動選擇該重載方法
20 // 2、如果沒有參數類型完全匹配的重載方法,首先會去匹配部分匹配的重載方法
21 // 注意:如果有多個部分匹配程度一致的重載方法,會ambiguous for the type語法錯誤,因為無法選擇
22 // 3、如果只有一個參數類型不匹配的方法,這時也就不用談什麼重載了,觀察形參的數據類型和實參的數據類型,如果形參的數據類型可以包含實參的數據類型就不會出錯
23 }
24
25 // 重載方法1
26 public static int test(int i, int j) {
27 return 1;
28 }
29
30 // 重載方法2
31 // public static int test(double i, double j) {
32 // return 2;
33 // }
34
35 // 重載方法3
36 // public static int test(int i, double j) {
37 // return 3;
38 // }
39
40 // 重載方法4
41 // public static int test(double i, int j) {
42 // return 4;
43 // }
44
45 // 重載方法5
46 // public static int test() {
47 // return 5;
48 // }
49
50 // 重載方法6
51 public static int test(long a, long b) {
52 return 6;
53 }
54 }