递归问题,方法传参(以对象为参数)
递归问题
递归,本质上是对自己方法的反复调用。
public class recursion01 {     public static void main(String[] args) {         System.out.println();         Tools tos = new Tools();         tos.test(30);          System.out.println(tos.factorial(5));     } }
  class Tools {     int numx=0;          public void test(int n) {         if (n>2) {             numx += 1;             test(n-1);         }                    System.out.println("n="+n+"numx="+numx);     }          
 
 
 
 
 
 
 
 
 
      public int factorial (int n) {         if (n==1) {                                       return 1;         }else {                                       return factorial(n-1)*n;         }     } }
  | 
 
test与阶乘的结果集:

下面引自老师的内存分析法:
调用test时

调用factorial时

方法传参(以对象为参数)
上课时听到的,方法调用对象作为参数时,会调用对象的地址,修改它的属性值时会影响到原先的属性值。但如果在引入对象作为参数时,将引入且创建出来的当前对象的地址指向别处时,当前对象的修改不会影响到原来被引入的对象。
public class word {     public static void main(String[] args) {                  Person p = new Person();                  p.name = "SYW";         p.age = 19;         System.out.println("未被改变的对象属性:"+p.name+" "+p.age);                  Temporary tmp = new Temporary();                                    tmp.force(p);         System.out.println("已被改变的对象属性:"+p.name+" "+p.age);     } }
  class Person {          String name;     int age; } 
  class Temporary {          public void force(Person p1) {                           p1.name = "Python";         p1.age = 133;
          
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
      } }
  | 
 
