正常方法调用 递归方法调用
    正常的方法调用过程.jpg递归方法的调用过程.jpg

    1. package newtestfile;
    2. public class TestMethod {
    3. // public void testOne(){
    4. // this.testTwo();
    5. // System.out.println("我是testOne方法");
    6. // }
    7. // public void testTwo(){
    8. // this.testThree();
    9. // System.out.println("我是testTwo方法");
    10. // }
    11. // public void testThree(){
    12. // System.out.println("我是testThree方法");
    13. // }
    14. //递归---->本质是方法的调用
    15. // 设计一个方法 描述盖宝塔
    16. // public void buildTower(int floor){//5
    17. // for(int i=1;i<=floor;i++){
    18. // System.out.println("盖到第"+floor+"层宝塔啦");
    19. // }
    20. // }
    21. //递归的想法
    22. // 想让我做事(盖第五层) 我要求让别人把之前的事情做完
    23. // 1.让别人先做事
    24. // 2.我自己做事
    25. public void buildTower(int floor){//5
    26. //判断当前floor是否为1 若不是找一个别人先盖之前层
    27. if(floor>1){
    28. this.buildTower(floor-1);
    29. }
    30. //我自己做事
    31. System.out.println("盖到第"+floor+"层宝塔啦");
    32. }
    33. // public void buildTower(int floor){//4
    34. // //找一个别人先盖3层
    35. // this.buildTower(floor-1);
    36. // //我自己做事
    37. // System.out.println("盖到第"+floor+"层宝塔啦");
    38. // }
    39. // public void buildTower(int floor){//3
    40. // //找一个别人先盖2层
    41. // this.buildTower(floor-1);
    42. // //我自己做事
    43. // System.out.println("盖到第"+floor+"层宝塔啦");
    44. // }
    45. // public void buildTower(int floor){//2
    46. // //找一个别人先盖1层
    47. // this.buildTower(floor-1);
    48. // //我自己做事
    49. // System.out.println("盖到第"+floor+"层宝塔啦");
    50. // }
    51. // public void buildTower(int floor){//1
    52. // //我自己做事
    53. // System.out.println("盖到第"+floor+"层宝塔啦");
    54. // }
    55. public static void main(String[] args){
    56. //加载TestMethod类的过程
    57. TestMethod tm = new TestMethod();
    58. tm.buildTower(5);//执行一次
    59. // tm.testOne();//调用testOne让其执行一遍--->栈内存中 临时
    60. }
    61. }