thinkinginjava學(xué)習(xí)筆記1全面解讀方法重載_第1頁
thinkinginjava學(xué)習(xí)筆記1全面解讀方法重載_第2頁
thinkinginjava學(xué)習(xí)筆記1全面解讀方法重載_第3頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、thinking in java學(xué)習(xí)筆記 1 全面解 讀方法重 載分類: java 2011-11-06 21:44 92 人閱讀 評論 (1) 收藏 舉報(bào) 之前學(xué)習(xí)C+時(shí)對于方法重載有了一點(diǎn)認(rèn)識,最近看了 java編程思想一書,對書中方法重載 的章節(jié)進(jìn)行了一番研 習(xí)發(fā)現(xiàn)之前對于方法重載的認(rèn)識很淺薄很片面,所以在此 總結(jié)下最近關(guān) 于方法重載的學(xué)習(xí)結(jié)果,希望能比 較全面的解讀下方法重載。一,方法重 載 的基本 認(rèn)識 :學(xué)習(xí)之前我 們需要了解什么是方法重 載,方法名相同而參數(shù)不同的方法既是方法重 載, 簡單 而言就是 讓類 以統(tǒng)一的方式 處理不同 類型數(shù)據(jù)的一種方法。 舉個(gè)例子就能直 觀的看出來了

2、, 例如:void show() ; void show(String s) 例中定義了名字同 為show的兩個(gè)方法,可以看出他 們的參數(shù)不同, 這即為方法重 載。那么方法重 載有什么作用呢?前面我 們已經(jīng)解讀過 構(gòu)造器,有 時(shí)程序中需要用到參數(shù)不同而 方法名相同的構(gòu)造器, 如果直接定 義那么就會 產(chǎn)生錯(cuò)誤,此時(shí)方法重 載就發(fā)揮 了很大的作用, 計(jì)算機(jī)可以通 過方法重載中傳遞的參數(shù)找到適合的構(gòu)造器 進(jìn)行調(diào)用,從而 實(shí)現(xiàn)功能。下面給出個(gè)簡單的例子加深下 認(rèn)識。java view plaincopyprint?class Tree int height;Tree() prt("Plant

3、ing a seedling"); height = 0;Tree(int i) prt("Creating new Tree that is "+ i + " feet tall");height = i;void info() prt("Tree is " + height+ " feet tall");void info(String s) prt(s + ": Tree is "+ height + " feet tall");static void prt

4、(String s) System.out.println(s);public class Overloading public static void main(String args) for(int i = 0; i < 5; i+) Tree t = new Tree(i);(); ("overloaded method");/ Overloaded constructor:new Tree(); /*Output:Creating new Tree that is 0 feet tallTree is 0 feet tallOverl

5、oading method: Tree is 0 feet tall Creating new Tree that is 1 feet tall Tree is 1 feet tallOverloading method: Tree is 1 feet tall Creating new Tree that is 2 feet tall Tree is 2 feet tallOverloading method: Tree is 2 feet tall Creating new Tree that is 3 feet tall Tree is 3 feet tallOverloading me

6、thod: Tree is 3 feet tall Creating new Tree that is 4 feet tall Tree is 4 feet tallOverloading method: Tree is 4 feet tall Planting a seeding*/ class Tree int height;Tree() prt("Planting a seedling");height = 0;Tree(int i) prt("Creating new Tree that is "+ i + " feet tall&qu

7、ot;);height = i;void info() prt("Tree is " + height+ " feet tall");void info(String s) prt(s + ": Tree is "+ height + " feet tall");static void prt(String s) System.out.println(s);public class Overloading public static void main(String args) for(int i = 0; i &

8、lt; 5; i+) Tree t = new Tree(i);(); ("overloaded method");/ Overloaded constructor:new Tree(); /*Output:Creating new Tree that is 0 feet tallTree is 0 feet tallOverloading method: Tree is 0 feet tallCreating new Tree that is 1 feet tallTree is 1 feet tallOverloading method: Tre

9、e is 1 feet tallCreating new Tree that is 2 feet tallTree is 2 feet tallOverloading method: Tree is 2 feet tallCreating new Tree that is 3 feet tallTree is 3 feet tallOverloading method: Tree is 3 feet tallCreating new Tree that is 4 feet tallTree is 4 feet tallOverloading method: Tree is 4 feet tal

10、l Planting a seeding*/創(chuàng)建 Tree 對象的 時(shí) 候,既可以不含參數(shù),也可以用 樹的高度當(dāng)參數(shù)。前者表示一 顆樹苗,后 者表示已有一定高度的 樹 木。要支持 這種 創(chuàng)建方式,得有一個(gè)默 認(rèn)構(gòu)造器和一個(gè)采用 現(xiàn)有高 度作 為參數(shù)的構(gòu)造器。通 過上述方法 對于方法重 載就有了基本的了解。 現(xiàn)在便要知道 該如何 區(qū)分重 載 方法。二,區(qū)分重 載 方法:根據(jù)方法重 載的定 義可以看出,方法重 載最大的區(qū) 別在于參數(shù)不同,于是就有了以下幾種區(qū) 分方法:1,參數(shù) 類 型不同:void show(String s) ;roshow(i nt a) 。2,參數(shù)數(shù)目不同:void show

11、(i nt a) ;void show(i nt a,i nt b) 。3,參數(shù) 順 序不同:void show(int a,String s) ; void show(String a) 。這 個(gè)內(nèi)容相比比 較簡單 所以就不用我 蒼 白的文字 贅 述 過多了。接下來將要分享下新學(xué)到的關(guān)于方法重 載的知 識點(diǎn)。三,基本 類 型的重 載 這是個(gè)比較特殊的重 載應(yīng)用,我們通過例程分析: java view plaincopyprint?public class PrimitiveOverloading static void prt(String s) System.out.print

12、ln(s); void f1(char x) prt("f1(char)"); void f1(byte x) prt("f1(byte)"); void f1(short x) prt("f1(short)"); void f1(int x) prt("f1(int)"); void f1(long x) prt("f1(long)"); void f1(float x) prt("f1(float)"); void f1(double x) prt("f1(do

13、uble)"); void f2(byte x) prt("f2(byte)"); void f2(short x) prt("f2(short)"); void f2(int x) prt("f2(int)"); void f2(long x) prt("f2(long)"); void f2(float x) prt("f2(float)"); void f2(double x) prt("f2(double)"); void f3(short x) prt(&

14、quot;f3(short)"); void f3(int x) prt("f3(int)"); void f3(long x) prt("f3(long)"); void f3(float x) prt("f3(float)"); void f3(double x) prt("f3(double)"); void f4(int x) prt("f4(int)"); void f4(long x) prt("f4(long)"); void f4(float x)

15、prt("f4(float)"); void f4(double x) prt("f4(double)"); void f5(long x) prt("f5(long)"); void f5(float x) prt("f5(float)"); void f5(double x) prt("f5(double)"); void f6(float x) prt("f6(float)"); void f6(double x) prt("f6(double)")

16、; void f7(double x) prt("f7(double)"); void testConstVal() prt( “ 5:"); f1(5);f2(5);f3(5);f4(5);f5(5);f6(5);f7(5);void testChar() char x = 'x' prt("char:");f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); void testByte() byte x = 0; prt("byte:");f1(x);f2(x);f3(x)

17、;f4(x);f5(x);f6(x);f7(x); void testShort() short x = 0; prt("short:");f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); void testInt() int x = 0;prt("int:"); f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);void testLong() long x = 0; prt("long:");f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);

18、f7(x); void testFloat() float x = 0; prt("float:");f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); void testDouble() double x = 0;prt("double:");f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);public static void main(String args) PrimitiveOverloading p =new PrimitiveOverloading();p.testCons

19、tVal();p.testChar(); p.testByte();p.testShort();p.testInt();p.testLong(); p.testFloat();p.testDouble(); /*Output5:f1(int) f2(int) f3(int) f4(int) f5(long) f6(float) f7(double) char: f1(char) f2(int) f3(int) f4(int) f5(long) f6(float) f7(double) byte: f1(byte) f2(byte) f3(short) f4(int) f5(long) f6(f

20、loat) f7(double) short: f1(short) f2(short) f3(short) f4(int) f5(long) f6(float) f7(double) int: f1(int) f2(int) f3(int) f4(int) f5(long) f6(float) f7(double) long: f1(long) f2(long) f3(long) f4(long) f5(long) f6(float) f7(double) float: f1(float) f2(float) f3(float) f4(float) f5(float) f6(float) f7

21、(double) double: f1(double) f2(double) f3(double) f4(double) f5(double) f6(double) f7(double)*/public class PrimitiveOverloading static void prt(String s) System.out.println(s);void f1(char x) prt("f1(char)"); void f1(byte x) prt("f1(byte)"); void f1(short x) prt("f1(short)&

22、quot;); void f1(int x) prt("f1(int)"); void f1(long x) prt("f1(long)"); void f1(float x) prt("f1(float)"); void f1(double x) prt("f1(double)"); void f2(byte x) prt("f2(byte)"); void f2(short x) prt("f2(short)"); void f2(int x) prt("f2(

23、int)"); void f2(long x) prt("f2(long)"); void f2(float x) prt("f2(float)"); void f2(double x) prt("f2(double)"); void f3(short x) prt("f3(short)"); void f3(int x) prt("f3(int)"); void f3(long x) prt("f3(long)"); void f3(float x) prt(&q

24、uot;f3(float)"); void f3(double x) prt("f3(double)"); void f4(int x) prt("f4(int)"); void f4(long x) prt("f4(long)"); void f4(float x) prt("f4(float)"); void f4(double x) prt("f4(double)"); void f5(long x) prt("f5(long)"); void f5(floa

25、t x) prt("f5(float)"); void f5(double x) prt("f5(double)"); void f6(float x) prt("f6(float)"); void f6(double x) prt("f6(double)"); void f7(double x) prt("f7(double)"); void testConstVal() prt( “ 5:"); f1(5);f2(5);f3(5);f4(5);f5(5);f6(5);f7(5);

26、void testChar() char x = 'x' prt("char:"); f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);void testByte() byte x = 0; prt("byte:"); f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); void testShort() short x = 0;prt("short:"); f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);void t

27、estInt() int x = 0;prt("int:"); f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);void testLong() long x = 0; prt("long:"); f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);void testFloat() float x = 0; prt("float:"); f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);void testDouble() double

28、x = 0; prt("double:");f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);public static void main(String args) PrimitiveOverloading p = new PrimitiveOverloading(); p.testConstVal();p.testChar();p.testByte();p.testShort();p.testInt();p.testLong();p.testFloat();p.testDouble(); /*Output5:f1(int) f2(int

29、) f3(int) f4(int) f5(long) f6(float) f7(double)char: f1(char) f2(int) f3(int) f4(int) f5(long) f6(float) f7(double) byte: f1(byte) f2(byte) f3(short) f4(int) f5(long) f6(float) f7(double) short: f1(short) f2(short) f3(short) f4(int) f5(long) f6(float) f7(double) int: f1(int) f2(int) f3(int) f4(int)

30、f5(long) f6(float) f7(double) long: f1(long) f2(long) f3(long) f4(long) f5(long) f6(float) f7(double) float: f1(float) f2(float) f3(float) f4(float) f5(float) f6(float) f7(double) double: f1(double) f2(double) f3(double) f4(double) f5(double) f6(double) f7(double)*/觀察這個(gè)程序的 輸出,就會 發(fā)現(xiàn) 常數(shù)值 5 被當(dāng)作一個(gè) int 值

31、處理。所以如果有某個(gè)重 載方法 接受 int 型參數(shù),他就會被 調(diào)用。再 觀察別的類型可以看出如果 傳入的數(shù)據(jù) 類型(實(shí)際參數(shù) 類 型)小于方法中聲明的形式參數(shù) 類型,實(shí)際 數(shù)據(jù)類型就會被提升。但注意 char 型不同,如果 無法找到適合 char 參數(shù)的方法, 那么 char 的提升將跳 過 byte 和 short 型而直接提升成 為 int 型。這是傳入?yún)?shù)小于形式參數(shù)的情況,那如果是 傳入的 實(shí)際參數(shù)大于重 載方法聲明的形式 參數(shù)將會是以下 這 種情況。java view plaincopyprint?public class Demotion static void prt(Stri

32、ng s) System.out.println(s);void f1(char x) prt("f1(char)"); void f1(byte x) prt("f1(byte)"); void f1(short x) prt("f1(short)"); void f1(int x) prt("f1(int)"); void f1(long x) prt("f1(long)"); void f1(float x) prt("f1(float)"); void f1(doub

33、le x) prt("f1(double)"); void f2(char x) prt("f2(char)"); void f2(byte x) prt("f2(byte)"); void f2(short x) prt("f2(short)"); void f2(int x) prt("f2(int)"); void f2(long x) prt("f2(long)"); void f2(float x) prt("f2(float)"); void

34、f3(char x) prt("f3(char)"); void f3(byte x) prt("f3(byte)"); 窄化 轉(zhuǎn)換void f3(short x) prt("f3(short)"); void f3(int x) prt("f3(int)"); void f3(long x) prt("f3(long)"); void f4(char x) prt("f4(char)"); void f4(byte x) prt("f4(byte)");

35、 void f4(short x) prt("f4(short)"); void f4(int x) prt("f4(int)"); void f5(char x) prt("f5(char)"); void f5(byte x) prt("f5(byte)"); void f5(short x) prt("f5(short)"); void f6(char x) prt("f6(char)"); void f6(byte x) prt("f6(byte)"

36、;); void f7(char x) prt("f7(char)"); void testDouble() double x = 0; prt("double argument:"); f1(x);f2(float)x);f3(long)x);f4(int)x); f5(short)x);f6(byte)x);f7(char)x);/ public static void main(String args) Demotion p = new Demotion(); p.testDouble(); /*Output: double argument: f

37、1(double) f2(float) f3(long) f4(int) f5(short) f6(byte) f7(char)*/ public class Demotion static void prt(String s) System.out.println(s);void f1(char x) prt("f1(char)"); void f1(byte x) prt("f1(byte)"); void f1(short x) prt("f1(short)"); void f1(int x) prt("f1(int)

38、"); void f1(long x) prt("f1(long)"); void f1(float x) prt("f1(float)"); void f1(double x) prt("f1(double)"); void f2(char x) prt("f2(char)"); void f2(byte x) prt("f2(byte)"); void f2(short x) prt("f2(short)"); void f2(int x) prt("f2(int)"); void f2(long x) prt("f2(long)"); void f2(float x) prt("f2(float)"); void f3(char x) prt("f3(char)"); void f3(by

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論