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 }