码迷,mamicode.com
首页 > 其他好文 > 详细

小白之旅6

时间:2019-08-05 23:14:30      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:范围   params   ESS   返回结果   表示   实参   lse   word   cti   

一. 方法

1.1 方法的好处

提高代码的复用性
隐藏了功能的实现细节

1.2 方法的组成

  • 权限(范围)修饰符
    修饰方法的可见范围,
    取值有:public、protected、[default]、private
  • 特殊修饰符
    使用特定的关键字能让该方法具有特殊含义的修饰符,
    取值:static、final、abstract、synchronized
  • 返回类型
    表示该方法在运行完毕后会返回一个指定类型的结果给调用者
  • 方法名
    方法的名字
  • 参数列表
    定义方法时,用于明确调用者在调用时必须传入数据的数据类型和个数
    调用方法时,实际传入方法参与运算的数据
  • 方法体
    方法具体执行的功能

1.3 方法的定义和调用

  • 定义:创建方法,明确该方法的返回类型、方法名、参数列表、方法体
  • 调用:使用方法,明确所调方法的方法名、参数列表

1.4 参数列表

  • 形式参数:形参,在方法定义时告知调用者调用方法时需要传入数据的数据类型和个数
  • 实际参数:实参,在方法调用时传入方法真正参与运算的数据

1.5 返回类型

表示该方法在运行完毕后会返回一个指定类型的结果给调用者

  • void,表示空,该方法在运行完毕后没有返回值
  • 非void,需要明确该方法返回的结果是何种类型的,此时在方法运行完毕后需要通过return关键字把运算完的结果返回给调用者。

1.6 return关键字

  • 返回类型为非void:用于将运算结果返回给调用者,这个返回结果的数据类型必须与返回类型一致
  • 返回类型为void:结束方法

注:一旦遇到了return,无论是否有数据的返回,方法都会立即结束。

二. 方法的重载

概念:在同一个类中,方法名相同参数列表不同的若干个方法就形成了方法的重载。
参数列表不同指的是参数的个数不同、类型不同、个数类型都不同、顺序不同

三. 可变参数

  • 概念:
    参数的个数是没有限制的_ 格式:
    数据类型...变量名_ 特点:
    可变参数的本质就是数组* 注:
    如果参数列表中除了有可变参数外还有其他参数,那么可变参数必须在参数列表的末尾

四. 值传递和引用传递

值传递:传递的数据类型是基本数据类型,这些数据存在于栈内存中,会随着方法的弹栈而被释放

<span class="xml"><span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-function"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>><span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-keyword"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>><span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>public<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"></<span class="hljs-title">span</span>></span> <span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-keyword"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>><span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>static<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"></<span class="hljs-title">span</span>></span> <span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-keyword"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>><span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>void<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"></<span class="hljs-title">span</span>></span> <span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-title"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>>main<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-params"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>>(String[] args)<span class="hljs-tag"></<span class="hljs-title">span</span>></span> <span class="hljs-tag"></<span class="hljs-title">span</span>></span>{
    <span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-keyword"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>><span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>int<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"></<span class="hljs-title">span</span>></span> a = <span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-number"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>><span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-number"</span>></span>3<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"></<span class="hljs-title">span</span>></span>;
    f(a);
    System.<span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-keyword"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>><span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>out<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"></<span class="hljs-title">span</span>></span>.println(a);
}
<span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-function"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>><span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-keyword"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>><span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>public<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"></<span class="hljs-title">span</span>></span> <span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-keyword"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>><span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>static<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"></<span class="hljs-title">span</span>></span> <span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-keyword"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>><span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>void<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"></<span class="hljs-title">span</span>></span> <span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-title"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>>f<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-params"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>>(<span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-keyword"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>><span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>int<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"></<span class="hljs-title">span</span>></span> num)<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"></<span class="hljs-title">span</span>></span>{
    num++;
}
结果:a还是<span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-number"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>><span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-number"</span>></span>3<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"></<span class="hljs-title">span</span>></span>
`<span class="hljs-tag"></<span class="hljs-title">pre</span>></span>

引用传递:传递的数据类型的引用数据类型,这些数据存在于堆内存中,随着方法的弹栈这些数据仍旧在堆中不会被释放

<span class="hljs-tag"><<span class="hljs-title">pre</span>></span>`<span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-function"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>><span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-keyword"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>><span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>public<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"></<span class="hljs-title">span</span>></span> <span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-keyword"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>><span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>static<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"></<span class="hljs-title">span</span>></span> <span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-keyword"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>><span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>void<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"></<span class="hljs-title">span</span>></span> <span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-title"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>>main<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-params"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>>(String[] args)<span class="hljs-tag"></<span class="hljs-title">span</span>></span> <span class="hljs-tag"></<span class="hljs-title">span</span>></span>{
    <span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-keyword"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>><span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>int<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"></<span class="hljs-title">span</span>></span>[] arr = {<span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-number"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>><span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-number"</span>></span>1<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"></<span class="hljs-title">span</span>></span>,<span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-number"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>><span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-number"</span>></span>2<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"></<span class="hljs-title">span</span>></span>,<span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-number"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>><span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-number"</span>></span>3<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"></<span class="hljs-title">span</span>></span>,<span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-number"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>><span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-number"</span>></span>4<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"></<span class="hljs-title">span</span>></span>};
    d(arr);
    System.<span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-keyword"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>><span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>out<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"></<span class="hljs-title">span</span>></span>.println(arr[<span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-number"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>><span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-number"</span>></span>0<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"></<span class="hljs-title">span</span>></span>]);
}
<span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-function"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>><span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-keyword"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>><span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>public<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"></<span class="hljs-title">span</span>></span> <span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-keyword"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>><span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>static<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"></<span class="hljs-title">span</span>></span> <span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-keyword"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>><span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>void<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"></<span class="hljs-title">span</span>></span> <span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-title"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>>d<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-params"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>>(<span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-keyword"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>><span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>int<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"></<span class="hljs-title">span</span>></span>[] arr)<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"></<span class="hljs-title">span</span>></span>{
    arr[<span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-number"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>><span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-number"</span>></span>0<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"></<span class="hljs-title">span</span>></span>]++;
}
结果:arr[<span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-number"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>><span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-number"</span>></span>0<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"></<span class="hljs-title">span</span>></span>]变成了<span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-number"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>><span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-number"</span>></span>2<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"></<span class="hljs-title">span</span>></span>
`<span class="hljs-tag"></<span class="hljs-title">pre</span>></span>

<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-preprocessor"</span>></span>## 五. 递归<span class="hljs-tag"></<span class="hljs-title">span</span>></span>

概念:方法直接或者间接地调用自身
问题:StackOverflowError:栈溢出
要素:  
`<span class="hljs-tag"></<span class="hljs-title">pre</span>></span></span>
`</pre>
  • 方法在调用的过程中,数据规模要逐渐减小

  • 两次方法调用之间应该有联系,上一次方法的返回是下一次方法的传参

  • 方法要有一个合理的程序出口,当到达程序出口时,递归结束
    案例:
    1.求1~100的累加

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> <span class="hljs-title">f</span><span class="hljs-params">(<span class="hljs-keyword">int</span> num)</span></span>{ <pre><span class="hljs-keyword">if(num == <span class="hljs-number">1){

    <span class="xml"><span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-keyword"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>><span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>return<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"></<span class="hljs-title">span</span>></span> <span class="hljs-tag"><<span class="hljs-title">span</span> <<span class="hljs-attribute">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-keyword"</span>></span>class<span class="hljs-tag"></<span class="hljs-title">span</span>></span>=<span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-string"</span>></span>"hljs-number"<span class="hljs-tag"></<span class="hljs-title">span</span>></span>><span class="hljs-tag"><<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"hljs-number"</span>></span>1<span class="hljs-tag"></<span class="hljs-title">span</span>></span><span class="hljs-tag"></<span class="hljs-title">span</span>></span>;</span>
    }<span class="hljs-keyword">else{
    `<span <span class="hljs-keyword">class</span>=<span class="hljs-string">"hljs-keyword"</span>><span class="hljs-keyword">return</span></span> num+f(num - <span <span class="hljs-keyword">class</span>=<span class="hljs-string">"hljs-number"</span>><span class="hljs-number">1</span></span>);
    }

}

2.求斐波那契数列的第20项

小白之旅6

标签:范围   params   ESS   返回结果   表示   实参   lse   word   cti   

原文地址:https://www.cnblogs.com/demonycw/p/11306168.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!