diff --git "a/14 \345\256\213\345\256\217\346\211\254/20221216\345\205\263\344\272\216\346\226\271\346\263\225\345\217\202\346\225\260\347\232\204\347\254\224\350\256\260\344\270\216\344\275\234\344\270\232.md" "b/14 \345\256\213\345\256\217\346\211\254/20221216\345\205\263\344\272\216\346\226\271\346\263\225\345\217\202\346\225\260\347\232\204\347\254\224\350\256\260\344\270\216\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..e6e2a3fb139a979151417acf121bb7ce3df52f11 --- /dev/null +++ "b/14 \345\256\213\345\256\217\346\211\254/20221216\345\205\263\344\272\216\346\226\271\346\263\225\345\217\202\346\225\260\347\232\204\347\254\224\350\256\260\344\270\216\344\275\234\344\270\232.md" @@ -0,0 +1,394 @@ +## 1.方法重载 + +- 方法重载概念:指同一个类中多个方法之间的关系,满足下列条件的多个方法相互构成重载 + + - 多个方法在同一个类中 + - 多个方法具有相同的方法名 + - 多个方法的参数不相同,类型不同或者数量不同 + +- 注意 + + - 重载仅对应方法的定义,与方法的调用无关,调用格式参考标准格式 + - 重载仅针对同一个类中方法的名称与参数进行识别,与返回值无关,即不能通过返回值来判定两个方法是否相互构成重载 + +- 范例 + + ```java + //正确范例 + public class S1 { + public static void fn(int a) { + //方法体 + } + public static int fn(double a) { + //方法体 + } + } + + public class S1 { + public static float fn(int a) { + //方法体 + } + public static int fn(int a , int b){ + //方法体 + } + } + + //错误范例 + public class S1 { + public static void fn(int a) { + //方法体 + } + public static int fn(int a) { //错误原因:重载与返回值无关 + //方法体 + } + } + + public class S1 { + public static void fn(int a) { + //方法体 + } + } + + public class S2 { + public static int fn(double a) { //错误原因:这是两个类的两个fn方法 + //方法体 + } + } + ``` + +## 2.方法重载练习 + +```java +//需求:使用方法重载思想,设计比较两个整数是否相同的方法,兼容全整数类型(byte,shout,int,long) + +public class S5 { + public static void main(String[] args) { + System.out.println(bijiao(10,20)); + System.out.println(bijiao((byte)(10),(byte)(10))); + System.out.println(bijiao((short)(10),(short)(20))); + System.out.println(bijiao(10L,20L)); + } + public static boolean bijiao (int a , int b) { + System.out.println("int"); + return a==b; + } + public static boolean bijiao (byte a ,byte b) { + System.out.println("byte"); + return a == b; + } + public static boolean bijiao (short a , short b) { + System.out.println("short"); + return a == b; + } + public static boolean bijiao (long a ,long b) { + System.out.println("long"); + return a == b; + } +} +``` + +## 3.方法的参数传递 + +### 1.方法参数传递基本类型 + +- 测试代码: + + ```java + public class 代码预览 { + /* + 方法参数传递为基本数据类型 : + 传入方法中的, 是具体的数值. + */ + public static void main(String[] args) { + int number = 100; + System.out.println("调用change方法前:" + number); + change(number); + System.out.println("调用change方法后:" + number); + } + public static void change(int number) { + number = 200; + } + } + ``` + +- 结论: + - 基本数据类型的参数,形式参数的改变不影响实际参数 +- 结论依据: + - 每个方法在栈内存中,都会有独立的栈空间,方法运行结束后就会弹栈消失 + +### 2.方法参数传递引用类型 + +- 测试代码 + + ```java + public class 代码预览 { + /* + 方法参数传递为引用数据类型 : + 传入方法中的, 是内存地址. + */ + public static void main(String[] args) { + int[] arr = {10, 20, 30}; + System.out.println("调用change方法前:" + arr[1]); + change(arr); + System.out.println("调用change方法后:" + arr[1]); + } + public static void change(int[] arr) { + arr[1] = 200; + } + } + ``` + +- 结论: + - 对于引用类型的参数,形式参数的改变,影响实际参数的值 +- 结论依据: + - 引用数据类型的传参传入的是地址值,内存中会造成两个引用指向同一个内存的效果,所以即使方法弹栈堆内存中的数据也已经是改变后的结果 + +## 4.练习题 + +### 1.数组遍历 + +```java +//需求:设计一个方法用于数组遍历,要求遍历的结果是在一行上的。例如:[11, 22, 33, 44, 55] + +public class S6 { + public static void main(String[] args) { + int[] arr = {11,22,33,44,55}; + printArr(arr); + } + public static void printArr(int[] arr) { + System.out.print("["); + for (int i = 0;i < arr.length;i++) { + if (i == arr.length -1){ + System.out.print(arr[i] + "]"); + } else { + System.out.print(arr[i] + " "); + } + } + } +} +``` + +### 2.数组最大值 + +```java +//需求:设计一个方法用于获取数组中元素的最大值 + +public class S7 { + public static void main(String[] args) { + int[] arr = {11,22,33,44,55}; + int max = getMax(arr); + System.out.println(max); + } + public static int getMax(int[] arr) { + int max = arr[0]; + for (int i = 0 ; i < arr.length ; i++) { + if (max < arr[i]) { + max = arr[i]; + } + } + return max; + } +} +``` + +### 3.方法同时获取数组最大值和最小值 + +```java +//需求:设计一个方法,该方法能够同时获取数组的最大值,和最小值 + +public class S8 { + public static void main(String[] args) { + int[] arr = {11,22,33,44,55}; + int[] maxAndmin = getmaxAndmin(arr); + System.out.println("最大值:" + maxAndmin[0]); + System.out.println("最小值:" + maxAndmin[1]); + } + public static int[] getmaxAndmin(int[] arr){ + int max = arr[0]; + int min = arr[0]; + for (int i = 0 ; i < arr.length ; i++){ + if (max < arr[i]){ + max = arr[i]; + } + if (min > arr[i]){ + min = arr[i]; + } + } + int[] maxAndmin = {max,min}; + return maxAndmin; + } +} +``` + +## 4.作业 + +### 1. + +```java +//需求:编写函数,计算圆的面积和周长,在主函数中接受圆的半径,在自定义函数中计算并输出 + +import java.util.Scanner; +public class S9 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入圆的半径:"); + double r = sc.nextDouble(); + getS(r); + getC(r); + } + public static void getS(double r) { + double S = r*r; + System.out.println("这个圆的面积为:" + S + "π"); + } + public static void getC(double r) { + double C = 2*r; + System.out.println("这个圆的周长为:" + C + "π"); + } +} +``` + +### 2. + +```java +//需求:在主函数中产生20个0-10之间的随机数,将这20个随机数存入数组,并通过函数计算某个数在这些随机数中出现的次数(这“某个数”是在主函数中由用户输入的) + +import java.util.Random; +import java.util.Scanner; +public class S10 { + public static void main(String[] args) { + Random ran = new Random(); + int[] arr = new int[20]; + System.out.print("随机数组为:"); + for (int i = 0 ; i < arr.length ; i ++){ + arr[i] = ran.nextInt(11); + } + for (int a = 0 ; a < arr.length ; a ++){ + System.out.print(arr[a] + " "); + } + Scanner sc = new Scanner(System.in); + System.out.println(); + System.out.println("请输入0-10中任意数字:"); + int number = sc.nextInt(); + System.out.println("数字" + number + "在数组中一共出现了" + cishu(number,arr) + "次!"); + } + public static int cishu(int number,int[] arr){ + int num = 0; + for (int i = 0 ; i < arr.length ; i ++) { + if (number == arr[i]) { + num ++; + } + } + return num; + } +} +``` + +### 3. + +```java +//需求:在主函数中接收10个数存入数组,在自定义函数中,将该数组中的最大值与第一个元素交换,最小值与最后一个元素交换,然后在主函数中输出交换后的数组 + +import java.util.Scanner; +public class S11 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int arr[] = new int[10]; + for (int i = 0 ; i < arr.length ; i ++) { + System.out.println("请输入第" + (i+1) + "个整数:"); + arr[i] = sc.nextInt(); + } + System.out.print("整理前的数组为:"); + for (int i = 0 ;i < arr.length ; i ++) { + System.out.print(arr[i] + " "); + } + System.out.println(); + int[] maxAndmin = getmaxAndmin(arr); + int a; + for (int i = 0 ; i < arr.length ; i ++){ + if (maxAndmin[0] == arr[i]) { + a = arr[0]; + arr[0] = maxAndmin[0]; + arr[i] = a; + } + if (maxAndmin[1] == arr[i]) { + a = arr[9]; + arr[9] = maxAndmin[1]; + arr[i] = a; + } + } + System.out.print("整理后的数组为:"); + for (int i = 0 ;i < arr.length ; i ++) { + System.out.print(arr[i] + " "); + } + } + public static int[] getmaxAndmin(int[] arr) { + int max = arr[0]; + int min = arr[0]; + for (int i = 0 ; i < arr.length ; i ++) { + if (max < arr[i]) { + max = arr[i]; + } + if (min > arr[i]){ + min = arr[i]; + } + } + int[] maxAndmin = {max,min}; + return maxAndmin; + } +} +``` + +### 4. + +```java +//需求:用自定义函数实现求某数组元素的和(大小不固定) + +import java.util.Random; +public class S12 { + public static void main(String[] args) { + Random ran = new Random(); + int arr[] = new int[5]; + for (int i = 0 ; i < arr.length ; i ++) { + arr[i] = ran.nextInt(90)+10; + } + System.out.print("随机数组为:"); + for (int i = 0 ; i < arr.length ; i ++) { + System.out.print(arr[i] + " "); + } + System.out.println(); + System.out.println("该随机数组的和为:" + number(arr)); + } + public static int number(int arr[]){ + int num = 0; + for (int i = 0 ; i < arr.length ; i++) { + num += arr[i]; + } + return num; + } +} +``` + +### 5. + +```java +//需求:用户输入整数n,计算1!+(1!+2!)+(1!+2!+3!)+…..+(1!+2!+…n!) + +import java.util.Scanner; +public class S13 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入n的值:"); + double num = sc.nextInt(); + System.out.println("1!+(1!+2!)+(1!+2!+3!)+…..+(1!+2!+…n!)=" + number(num)); + } + public static double number(double num) { + double he = 0; + for (int i = 1 ; i <= num ; i ++) { + for (int j = 1 ; j <= i ; j ++) { + he += j; + } + } + return he; + } +} +``` \ No newline at end of file