package com.test.day01; // public class TestParam { public void f1(int n){ n =0; } public static void main(String[] args) { TestParam testParam = new TestParam(); int n = 9; testParam.f1(n); System.out.println(n); } }
思考:上面的代码运行结果输出的是0还是9 答案是9 因为调用一个方法就会在栈里面开辟空间,这时候0在一个空间 9在一个空间。那么结果不会随着方法的调用而改变
那么什么时候会改变呢?
package com.test.day01; // class Param{ int value; } public class TestParam {
//讲Param类作为类型
public void f2(Param p){ p.value=99; } public static void main(String[] args) { TestParam testParam = new TestParam(); Param pc = new Param(); pc.value=22; testParam.f2(pc); System.out.println(pc.value); } }
思路:引用传递是地址的传递,这里面使用了同一个地址,也就是都指向99 所以结果是99 如果在 f2里面再次创建一个对象的话,就会在堆里面创建对象,就会有新的地址。结果就会是22
使用引用参数求5个数的和:
package com.test.day01; // public class TestParam { //创建一个求和的方法 public int num(int n1,int n2,int n3,int n4,int n5){ return n1+n2+n3+n4+n5; } public static void main(String[] args) { //求5个数的和 TestParam tp = new TestParam(); int sum = tp.num(88, 99, 54, 78, 90); System.out.println(sum); } }
上述的代码虽然完成5个数的求和,但是如果遇到很多个数求和那么显然这样的方式是不可取的。那么怎么解决呢?传参的时候使用引用数据类型数组然后for循环的方式;
package com.test.day01; // public class TestParam { //创建一个求和的方法 //定义一个求和的方法 public int sum(int [] arr){ int sum =0; for (int i = 0; i < arr.length; i++) { sum += arr[i]; } return sum; } public static void main(String[] args) { TestParam testParam = new TestParam(); int [] a={6542,3,5,7,4,2,4,67,8,3,56,8,534,3,2,6,6,8,9,4,2}; int num = testParam.sum(a); System.out.println(num); } }
定义一个数组,存储3名学员的基本信息,编写方法计算学员的总成绩。
package com.test.day01; class Student{ //姓名 性别 年龄 分数 double scoer; char sex; String name; int age; } //管理类 class Manager{ public double sum(Student [] stus){ int sum =0; for (int i = 0; i < stus.length; i++) { sum += stus[i].scoer; } return sum; } } public class TestStudent { public static void main(String[] args) { // TODO Auto-generated method stub Student xiaolongnv = new Student(); xiaolongnv.scoer=100; Student mucheng = new Student(); mucheng.scoer=84.5; Student zzhangruocheng = new Student(); zzhangruocheng.scoer=69.3; Student [] stus={xiaolongnv,mucheng,zzhangruocheng}; double num = new Manager().sum(stus); System.out.println(num); } }
可变参数
可变参数的底层是数组 他的优点是传参灵活,注意多个参数的时候可变参数只能放在最后。
public class TestVarParam { public void f(String s ,int [] arr,int [] arr1) { System.out.println(Arrays.toString(arr)); } public void ff(int ...arr ) {// int []arr System.out.println(Arrays.toString(arr)); System.out.println(arr.length); } public static void main(String[] args) { TestVarParam test = new TestVarParam(); // 1 // int [] arr = {11,22,33}; // int [] arr = new int[]{11,22,33}; // test.f(arr); // 2 // test.f(new int [] {11,22,33}); // 3. test.ff();// new int []{}; 长度为0 的数组 test.ff(111);// new int[]{111} test.ff(11,22,33,44);// new int[]{11,22,33,44}; test.ff(new int[] {11,22}); } }
递归
递归就是方法自身调用自身,但是一定要有出口
package com.test.day01; public class Recursion { int count=0; public void f(){ count++; if(count==10){ return; } System.out.println("慢慢学,这要学的快"); f(); } public static void main(String[] args) { // 使用递归的方法输出10个“慢慢学,这要学的快” Recursion re = new Recursion(); re.f(); } }
使用递归实现阶乘
package com.test.day01; public class Factorial { public int fa(int n){ if(n==1){ return 1; }else{ return n*fa(n-1); } } public static void main(String[] args) { // 使用递归实现阶乘 阶乘形式:1*2*3*4*5*6*7 Factorial factorial = new Factorial(); System.out.println(factorial.fa(6)); } }