第1章 方法重载OverLoad 1.2 概念
方法重载 :指在同一个类中,允许存在一个以上的同名方法,只要它们的参数列表不同即可,与修饰符和返回值类型无关。
多个方法在同一个类中
多个方法具有相同的方法名
多个方法的参数不相同,类型不同或者数量不同
注意
参数列表:个数不同,数据类型不同,顺序不同 。
重载方法调用:JVM通过方法的参数列表,调用不同的方法。
使用方法重载完成以上练习
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 public class Demo02MethodOverLoad { public static void main (String[] args) { System.out.println(getSum(10 ,20 )); System.out.println(getSum(10 ,20 ,30 )); System.out.println(getSum(10.0 ,20.0 )); System.out.println(getSum(10.0 ,20.0 ,30.0 )); } public static int getSum (int a, int b) { System.out.println("求两个int数字之和...." ); return a + b; } public static int getSum (int a, int b,int c) { System.out.println("求三个int数字之和...." ); return a + b + c; } public static double getSum (double a, double b) { System.out.println("求两个double数字之和...." ); return a + b; } public static double getSum (double a, double b,double c) { System.out.println("求三个double数字之和...." ); return a + b + c; } }
1.2 方法重载注意事项 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 1. 定义方法method,没有参数public static void method () {}2. 定义方法method,有一个int 参数public static void method (int a) {}3. 定义方法method,有一个double 参数public static void method (double a) {}4. 定义方法method,有一个int 参数和一个double 参数public static void method (int a, double b) {}5. 定义方法method,有一个double 参数和一个int 参数public static void method (double a, int b) {} public static int method (int a, double b) { return 0 ; } public static double method (int a, double b) { return 0.0 ; } public static int method (int c, double d) { return 0 ; } public int method (int a, double b) { return 0 ; } public static void main (String[] args) { fire(); fire("岛国2" ); fire("米国" , 999 ); } public static void fire () { fire("岛国" ); } public static void fire (String country) { fire(country, 1 ); } public static void fire (String country, int number) { System.out.println("发射了" + number + "枚武器给" + country); }
1.4 方法重载练习
1 2 3 4 5 6 7 8 9 public static void open () {}public static void open (int a) {}static void open (int a,int b) {}public static void open (double a,int b) {}public static void open (int a,double b) {}public void open (int i,double d) {}public static void OPEN () {}public static void open (int i,int j) {}
第2章 方法的参数传递 2.1 概念
传递给方法中的参数,这样方法中的参数就拥有了这个指定的值,可以使用该值,在方法中运算了。这种传递方式,我们称为参数传递。
参数列表中的变量,我们称为形式参数
调用方法时,传入给方法的数值,我们称为实际参数
2.2 基本数据类型作为方法参数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 public class Demo03BaseVar { public static void main (String[] args) { int a = 10 ; int b = 20 ; System.out.println("ms..a=" +a); System.out.println("ms..b=" +b); change( a, b ); System.out.println("me..a=" +a); System.out.println("me..b=" +b); } public static void change (int a, int b) { System.out.println("cs..a=" +a); System.out.println("cs..b=" +b); a = a * 10 ; b = b * 10 ; System.out.println("ce..a=" +a); System.out.println("ce..b=" +b); return ; } }
注意:形式参数的改变不影响实际参数
2.4 引用数据类型作为方法参数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 public class Demo04RefVar { public static void main (String[] args) { int [] a = new int [] {10 ,20 }; System.out.println("ms..a[0]=" +a[0 ]); System.out.println("ms..a[1]=" +a[1 ]); change( a ); System.out.println("me..a[0]=" +a[0 ]); System.out.println("me..a[1]=" +a[1 ]); } public static void change (int [] a) { System.out.println("cs..a[0]=" +a[0 ]); System.out.println("cs..a[1]=" +a[1 ]); a[0 ] = a[0 ] * 10 ; a[1 ] = a[1 ] * 10 ; System.out.println("ce..a[0]=" +a[0 ]); System.out.println("ce..a[1]=" +a[1 ]); return ; } }
注意:引用类型,作为方法参数,形式参数的改变会影响实际参数
2.6 双色球 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 package com.zxq._04_双色球;import java.util.Random;import java.util.Scanner;public class Test04 { public static void main (String[] args) { int [] arr = init(); for (int i : arr) { System.out.print(i+" " ); } int [] arr2 = userBuy(); System.out.println(); String kj = kj(arr, arr2); System.out.println(kj); } public static String kj (int [] system,int [] user) { boolean lanRes = system[0 ] == user[0 ]; int count = 0 ; for (int i = 1 ; i < user.length; i++) { int v = user[i]; boolean red = findRedByKey(system, 1 , system.length, v); if (red){ count++; } } System.out.println("蓝色球的结果:" +lanRes); System.out.println("红色球的结果:" +count); if (lanRes && count == 6 ){ return "1等奖" ; } if (count == 6 ){ return "2等奖" ; } if (lanRes && count == 5 ){ return "3等奖" ; } if (count == 5 || (lanRes && count == 4 )){ return "4等奖" ; } if (count == 4 || (lanRes && count == 3 )){ return "5等奖" ; } if (lanRes){ return "6等奖" ; } return "感谢您为中国福利彩票事业作出了贡献..." ; } public static int [] init() { int [] arr = new int [7 ]; Random random = new Random (); int randomCode = random.nextInt(1 , 17 ); arr[0 ] = randomCode; for (int i = 1 ; i < arr.length; ) { int h = random.nextInt(1 , 34 ); boolean exists = findRedByKey(arr,1 ,i,h); if (exists){ continue ; } arr[i] = h; i++; } return arr; } private static boolean findRedByKey (int [] arr, int min, int max, int key) { for (int i = min; i < max; i++) { if (arr[i] == key){ return true ; } } return false ; } public static int [] userBuy(){ Scanner sc = new Scanner (System.in); int [] arr = new int [7 ]; while (true ){ System.out.println("亲,请填写要购买的蓝色球号码:" ); int lan = sc.nextInt(); if (lan >= 1 && lan <= 16 ){ arr[0 ] = lan; break ; }else { System.out.println(lan+"不合法,请重新填写[1,16]之间的数字..." ); } } for (int i = 1 ; i < arr.length; ) { System.out.println("亲,请填写要购买第" +i+"个红色球号码:" ); int h = sc.nextInt(); if (h >= 1 && h <= 33 ){ boolean red = findRedByKey(arr, 1 , i, h); if (red){ System.out.println(h+"重复了,请重新填写[1,33]之间的数字..." ); }else { arr[i] = h; i++; } }else { System.out.println(h+"不合法,请重新填写[1,33]之间的数字..." ); } } return arr; } }
第3章 可变参数 3.1 可变参数的基本使用
可变参数是一种特殊的形式参数,定义在方法、构造器的形参列表处,它可以让方法接收多个同类型的实际参数。
可变参数在方法内部, 本质上是一个数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 可变参数: 可以变化的参数 在JDK1.5 之后,如果我们定义一个方法需要接受多个参数,并且多个参数类型一致,我们可以对其简化. 格式: 一定是在定义方法参数时,使用: 数据类型 ... 变量名称 修饰符 返回值类型 方法名称(数据类型 ... 变量名称) {...} 使用: 可变参数的本质就是数组,调用含有可变参数的方法时, 可以传递参数列表,可以传递数组,还可以不传参数 public class Test01 { public static void main (String[] args) { method(2 ,4 ,6 ,8 ,9 ); method(2 ,4 ); method(1 ); } public static void method (int a,int b) { System.out.println("2个参数的方法..." +a+"," +b); } public static void method (int ... a) { System.out.println("可变参数的方法..." +a); for (int i : a) { System.out.println(i); } } }
3.2 注意事项
最后还有一些错误写法
**一个形参列表中,只能有一个可变参数 ****;否则会报错**
一个形参列表中**如果多个参数 ,可变参数需要写在最后;否则会报错**
第4章 循环嵌套 4.1 需求: 打印3行5列的星 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 public class Demo07ForFor { public static void main (String[] args) { for (int i = 1 ; i <= 3 ; i++) { for (int j = 1 ; j <= 5 ; j++) { System.out.print("*" ); } System.out.println(); } } }
4.2标号语句 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class Test05 { public static void main (String[] args) { abcd:for (int i = 1 ; i <= 3 ; i++) { for (int j = 1 ; j <= 3 ; j++) { if (j==2 ){ continue abcd; } System.out.println("内循环的j=" +j); } System.out.println("外循环的i=" +i); } System.out.println("main方法介绍" ); } }
4.3 练习 使用嵌套循环,打印2021年至2023年月份,格式:xxxx年x月
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public class Demo08ForForMonth { public static void main (String[] args) { for (int year = 2021 ; year <= 2023 ; year++) { for (int month = 1 ; month <= 12 ; month++) { System.out.println(year + "年" + month + "月" ); } } } }
第5章 递归 5.1 介绍 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 方法递归 1. 概念: 方法自己调用自己 2. 死递归: 永不休止的自己调用自己 public static void method (int num) { System.out.println(num); num--; method(num); } 3. 正常递归: 递归次数不宜过多,可以正常结束 public static void method (int num) { System.out.println(num); num--; if (num == 0 ) { return ; } method(num); } 4. 注意: (1 )方法在栈内存中执行,栈内存空间有大小限制,所以 方法递归调用次数不宜过多,否则出栈内存溢出 (2 )方法的调用是先进后出的特点,上面的方法不弹栈, 下面的方法也不能弹栈,上面的方法不结束,下面的方法也不能结束 (3 )要想进行正常的递归操作必须找到递归调用的规律 (4 )要想进行正常的递归操作必须找递归调用的出口
1 2 3 4 5 6 7 8 9 10 11 12 13 public class Demo04DiGui { public static void main (String[] args) { method(10 ); } public static void method (int num) { System.out.println(num); num--; if (num == 0 ) { return ; } method(num); } }
5.2 递归求和 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 public class Demo05DiGuiSum { public static void main (String[] args) { int result = sum(4 ); System.out.println(result); } public static int sum (int n) { if (n == 1 ) { return 1 ; } return n + sum(n-1 ); } }
5.3 递归求和图解分析
5.4 递归获取文件夹下所有文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 public class Demo03EachTxtFiles { public static void main (String[] args) { File dir = new File ("day11_xw\\io" ); printNames(dir); } public static void printNames (File dir) { File[] files = dir.listFiles(); if (files != null && files.length > 0 ) { for (File file : files) { if (file.isFile()) { if (file.getName().toLowerCase().endsWith(".txt" )) { System.out.println(file.xiaoFeiName()); } } else { printNames(file); } } } } }
5.5 递归获取文件夹下所有文件图解分析