方法的声明和重载问题7

    技术2022-07-11  90

    知识点1:方法的声明

    * 类中方法的声明测试 * 1. 之前用过的一些方法: * Scanner类的nextInt() \ next() * Math类的random() \ sqrt(double d) * Arrays类的equals(int[] arr1,int[] arr2) \ toString(int[] arr) \ sort(int[] arr) * \ binarySearch(int[] arr,int value) * * 2. 类中方法声明的格式: * 权限修饰符 返回值类型 方法名(形参列表){ * 方法体 * } * 说明:关于方法中的特殊关键字:static\final\abstract\...暂时先不考虑。 * * 3. 方法声明的详细说明: * 3.1 权限修饰符:指名当前方法被调用权限的大小。 * 涉及到的权限修饰符有:private 、 缺省 、 protected 、 public。 详细细节见《封装性》 * 目前,大家在声明方法时,默认使用public即可。 * 3.2 返回值类型 : * > 可以分为:有具体的返回值类型 vs 没有返回值类型:void * > 有具体的返回值类型,可以使用任何的变量类型。比如:基本数据类型、引用数据类型 * > 有具体的返回值类型的方法,在调用方法结束后,一定要返回满足要求的返回值类型的变量或常量 * 补充:return的使用: * 在有具体返回值类型的方法中,一定需要使用“return + 变量/常量”的方法给方法返回指定的数据。 * 如果方法没有返回值类型,也可以使用"return;"结构,表示结束当前方法的执行。 * * 3.3 方法名:属于标识符,命名时满足标识符命名的规则和规范。“见名知意” * 3.4 形参列表: * 格式:参数类型1 参数名1,参数类型2,参数名2,... * 方法在声明时,不要给形参列表的变量赋值。当我们调用方法时,才给形参列表赋值:实参。 * 3.5 方法体:执行方法时,主要操作的逻辑。 * 4. 如何确定定义一个方法时,要不要声明返回值类型?要不要声明形参? * ① 看题目要求。 * ② 具体问题具体分析。 代码演示 public class PersonTest { public static void main(String[] args) { Person p = new Person(); p.printNumber(); int hour = 8; p.sleep(hour); System.out.println("##############"); p.eat(); } } class Person{ String name; int age; Account acct; public void printNumber(){ for(int i = 0;i < 100;i++){ if(i == 40){ return;//用于结束方法 } System.out.println(i); } System.out.println("hello!"); } public Account getAccount(){ boolean flag = true; if(flag){ return null; } return new Account(); } public int getValue(){ return 1; // return age; } public void sleep(int hour){ System.out.println("昨天睡了" + hour + "小时"); } public void eat(){ System.out.println("人需要吃饭,补充营养"); System.out.println("name = " + name); sleep(10); // eat(); } } class Account{//账户 } 补充说明 * 5. > 可以在当前类的方法中使用当前类定义的属性或其他的方法 * > 方法内不能定义新的方法。 课后练习 /** * 2.利用面向对象的编程方法,设计类Circle计算圆的面积。 * * @author tzm * @create 2020-07-01 11:37 */ public class CircleTest { public static void main(String[] args) { Circle c1 = new Circle(); c1.radius = 1.2; //对应方式一 // double area = c1.findArea(); // System.out.println("圆的半径为:" + c1.radius + ",面积为:" + area); //对应方式二: c1.findArea(); } } class Circle{//圆 double radius;//半径 //求圆的面积的方法 //方式一: // public double findArea(){ // return Math.PI * radius * radius; // } //方式二: public void findArea(){ System.out.println(Math.PI * radius * radius); } //错误的: // public double findArea(double r){ // return Math.PI * r * r; // } }

    知识点2:方法的重载

    概念的理解 * 1. 什么是方法的重载?同一个类中,相同方法名,不同参数列表的方法之间构成重载。 * * “两同一不同”:同一个类,相同方法名;参数个数 或 参数类型不同 * * 2. 方法的重载与权限修饰符、返回值类型、形参名没有关系! 代码演示 public class OverLoadTest { public static void main(String[] args) { OverLoadTest test = new OverLoadTest(); test.show(); test.show(1,2); } public void show(){ System.out.println("hello"); } public void show(int a){ System.out.println("萨瓦迪卡"); } void show(String info){ System.out.println(info); } public int show(int i,int j){ return i + j; } private void show(int i,String info){ } public void show(String info,int i){ } // public void show(String info1,int j){ // // } // public String show(String info,int i){ // return info + i; // } } 重点 * 3. 我们在调用类中的方法时,是如何确定调用的是某一个确定的方法呢? * 通过方法名确定 ---> 进一步通过形参列表确定

    知识点3:可变形参的方法

    /** * 可变形参方法的使用 * * 1. JDK5.0新增的特性。 * 2. 格式为:形参类型 ... 形参名 * 3. 当调用可变形参的方法时,实参可以为0个,1个,2个,。。。 * 4. 可变形参的方法与方法同名的其他方法之间构成重载(排除第5点) * 5. 一个类中,不能同时出现可变形参的方法 和 与其方法名相同且可变形参类型相同的数组的方法。 * 6. 如果一个方法中声明有可变形参,则可变形参一定声明在方法参数的最后。 * 7. 一个方法的形参中,最多声明一个可变个数的形参结构。 * * * @author tzm * @create 2020-07-01 14:59 */ public class ArgsTest { public static void main(String[] args) { // int[] arr = new int[0]; ArgsTest test = new ArgsTest(); // test.show(); // test.show("abc"); test.show("abc","123","xyz"); // test.show(new String[]{"abc","def"}); } public void show(){ System.out.println("show()"); } public void show(String s){ System.out.println("show(String s)"); } public void show(int i){ System.out.println("show(int i)"); } public void show(String ... info){ System.out.println("show(String ... info)"); for(int i = 0;i < info.length;i++){ System.out.println(info[i]); } } // public void show(String[] info){ // for(int i = 0;i < info.length;i++){ // System.out.println(info[i]); // } // } //错误的 // public void show(String ... info,String i){ // // } }

    知识点4:方法参数的值传递机制(重难点)

    1. 变量的传递机制:

    * 1. 如果传递的是基本数据类型的变量,则将变量本身保存的数据值传递过去 * 2. 如果传递的是引用数据类型的变量,则将变量本身保存的地址值传递过去 public class VariableTest { public static void main(String[] args) { int m = 10; //对于基本数据类型变量来说: int n = m; n = 20; System.out.println("m = " + m);//10 //对于引用数据类型变量来说: Order o1 = new Order(); o1.num = 10; // Order o2 = o1; o2.num = 20; System.out.println("o1.num = " + o1.num); // int[] array1 = new int[]{2,3,5,7,11}; int[] array2 = array1; array2[0] = 0; System.out.println(array1[0]);//0 } } class Order{ int num; }

    2. 方法形参的值传递机制

    * 1. 形参:方法声明时,小括号内的参数。 * 实参:方法调用时,实际传递给形参的数据 * * 2. 如果方法的形参是基本数据类型的变量,则将实参保存的数据值传递给形参变量。 * 如果方法的形参是引用数据类型的变量,则将实参保存的地址值传递给形参变量。 基本数据类型 public class ValueTransferTest { public static void main(String[] args) { int m = 10; int n = 20; System.out.println("m = " + m + ", n = " + n); //交换两个变量的值 // int temp = m; // m = n; // n = temp; ValueTransferTest test = new ValueTransferTest(); test.swap(m,n); System.out.println("m = " + m + ", n = " + n); } public void swap(int m ,int n){ int temp = m; m = n; n = temp; } }

    对应图示:

    引用数据类型 public class ValueTransferTest1 { public static void main(String[] args) { Data data = new Data(); data.m = 10; data.n = 20; System.out.println("m = " + data.m +", n = " + data.n); //交换的操作 ValueTransferTest1 test = new ValueTransferTest1(); test.swap(data); System.out.println("m = " + data.m +", n = " + data.n); System.out.println(data);//com.atguigu.java1.Data@1540e19d // User user = data; } public void swap(Data data1){ int temp = data1.m ; data1.m = data1.n; data1.n = temp; } } class Data{ int m ; int n; } class User{ }

    对应图示:

    3. 练习1

    /** * @author tzm * @create 2020-07-01 16:23 */ public class ValueTransferTest2 { public static void main(String[] args) { int[] arr = new int[]{34, 4, 2, 6, 6, 4, 7, -87, 0, 55, 98}; System.out.println(Arrays.toString(arr)); ValueTransferTest2 test = new ValueTransferTest2(); test.sort(arr, "ascend"); System.out.println(Arrays.toString(arr)); } public void sort(int[] arr, String sortMethod) { if ("ascend".equals(sortMethod)) {//从小到大 for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - 1 - i; j++) { if (arr[j] > arr[j + 1]) { //最初的写法:正确 // int temp = arr[j]; // arr[j] = arr[j + 1]; // arr[j + 1] = temp; //错误的写法 // swap(arr[j], arr[j + 1]); //正确的写法 swap(arr,j,j+1); } } } } else if ("descend".equals(sortMethod)) {//从大到小 for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - 1 - i; j++) { if (arr[j] < arr[j + 1]) { //最初的写法:正确 // int temp = arr[j]; // arr[j] = arr[j + 1]; // arr[j + 1] = temp; //错误的写法 // swap(arr[j], arr[j + 1]); //正确的写法 swap(arr,j,j+1); } } } } else { System.out.println("排序方式错误!"); } } // public void swap(int i,int j){ // int temp = i; // i = j; // j = temp; // } public void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } }

    4.练习2:

    public class TransferTest3 { public static void main(String args[]) { TransferTest3 test = new TransferTest3(); test.first(); } public void first() { int i = 5; Value v = new Value(); v.i = 25; second(v, i); System.out.println(v.i); } public void second(Value v, int i) { i = 0; v.i = 20; Value val = new Value(); v = val; System.out.println(v.i + " " + i); } } class Value { int i = 15; }

    对应的图示:

    补充内容:

    NullPointerException的理解

    /** * 关于面向对象内容中的NullPointerException的说明: * 凡是引用数据类型的变量(对象、数组),如果取值为null时,通过此变量调用其内部结构的话,一定就是 * NullPointerException的异常。 * * @author tzm * @create 2020-07-01 9:15 */ public class NullPointerExceptionTest { public static void main(String[] args) { User u1 = new User(); // u1 = null; // u1.name = "孔"; // u1.show(); System.out.println(u1.name); // System.out.println(u1.name.toString()); } } class User{ String name; int age; public void show(){ String str = null; System.out.println("我是一个用户"); } }

    对象数组的理解

    /** * 定义类Student,包含三个属性:学号number(int),年级state(int),成绩score(int)。 * 创建20个学生对象,学号为1到20,年级和成绩都由随机数确定。 * 问题一:打印出3年级(state值为3)的学生信息。 * 问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息 * * 提示: * 1) 生成随机数:Math.random(),返回值类型double; * 2) 四舍五入取整:Math.round(double d),返回值类型long。 * * @author tzm * @create 2020-07-01 11:56 */ public class StudentArrayTest { public static void main(String[] args) { int[] arr = new int[10]; String[] arr1 = new String[10]; // 创建20个学生对象 Student[] stus = new Student[20];//虚位以待 for (int i = 0; i < stus.length; i++) { stus[i] = new Student(); //给每一个学生的属性赋值 //学号 stus[i].number = i + 1; //年级:1-6 stus[i].state = (int)(Math.random() * 6 + 1); //成绩:0-100 stus[i].score = (int)(Math.random() * 101); } //问题一:打印出3年级(state值为3)的学生信息。 // for (int i = 0; i < stus.length; i++) { // if(stus[i].state == 3){ // stus[i].info(); // } // } //问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息 for(int i = 0;i < stus.length - 1;i++){ for(int j = 0;j < stus.length - 1 - i;j++){ if(stus[j].score > stus[j + 1].score){ Student temp = stus[j]; stus[j] = stus[j + 1]; stus[j + 1] = temp; } } } for (int i = 0; i < stus.length; i++) { stus[i].info(); } } } class Student{ int number;//学号 int state;//年级 int score;//成绩 public void info(){ System.out.println("number : " + number + ", state : " + state + ", score : " + score); } } 对应的内存解析

    Processed: 0.011, SQL: 9