标签:范围 params ESS 返回结果 表示 实参 lse word cti
提高代码的复用性
隐藏了功能的实现细节
表示该方法在运行完毕后会返回一个指定类型的结果给调用者
注:一旦遇到了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
<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项
标签:范围 params ESS 返回结果 表示 实参 lse word cti
原文地址:https://www.cnblogs.com/demonycw/p/11306168.html