重載

方法的重載

當(dāng)創(chuàng)建一個(gè)對(duì)象時(shí),就是給此對(duì)象分配到的存儲(chǔ)空間取了一個(gè)名字。使用名字可以引用所有的對(duì)象和方法。方法則是給某個(gè)動(dòng)作取得名字。

class Tree {
  int height;
    Tree() {
      System.out.println( " Planting a seeding " );
        height = 0;
      }
    Tree( int initialHeight ) {
      height = initialHeight;
      System.out.println( " Creating new Tree that is " + height       + " feet tall " );
      }
    void info() {
      System.out.println( " Tree is " + height + " feet tall " );
    }
    void info( String s ) {
      System.out.println( s + " : Tree is " + height + " feet tall " );
    }
}
public class OverLoading {
    public static void main( String[] args ) {
      for( int i = 0; i<5; i++ ) {
        Tree t = new Tree( i );
        t.info();
        t.info( " overloading method " );
        new Tree();
      }
    }   
}```

    這里用到了相同名字的兩個(gè)構(gòu)造器,這就叫重載。

用到了兩個(gè)構(gòu)造器:一個(gè)默認(rèn)構(gòu)造器,一個(gè)取字符串作為形式參數(shù)——該字符串表示初始化對(duì)象所需的文件名稱。都是構(gòu)造器,有相同的名字。

    函數(shù)重載的最重要目的是因?yàn)樗麄兌荚谧鐾患虑椋m然輸入不同,但是做的事情相同

***
###區(qū)分重載方法
每個(gè)重載方法都有自己獨(dú)一無二的**參數(shù)類型列表**。
```java
public class OverLoadingOrder {
    static void f( String s, int i ) {
      System.out.println( " String:  " + s + " , int: " + i );
    }
      static void f( int i, String s ) {
        System.out.println( " int: " + i + " , String: " + s );
      }
  public static void main( String[] args ) {
    f ( " String first ", 11 );
    f ( 99, " Int first " );
  }
}```
甚至參數(shù)順序不同也可以區(qū)分開。
***
###基本類型的重載
基本類型從“較小”的類型自動(dòng)提升到一個(gè)“較大”的類型,此過程一旦牽涉到重載,可能會(huì)造成一些混淆。
```java
public class PrimitiveOverLoading79 {
    void f1( char x ) { System.out.print( " f1( char ) " ); }
    void f1( byte x ) { System.out.print( " f1( byte ) " ); }
    void f1( short x ) { System.out.print( " f1( short ) " ); }
    void f1( int x ) { System.out.print( " f1( int ) " ); }
    void f1( long x ) { System.out.print( " f1( long) " ); }
    void f1( float x ) { System.out.print( " f1( float ) " ); }
    void f1( double x ) { System.out.print( " f1( double ) " ); }
    void f2( byte x ) { System.out.print( " f2( byte ) " ); }
    void f2( short x ) { System.out.print( " f2( short ) " ); }
    void f2( int x ) { System.out.print( " f2( int ) " ); }
    void f2( long x ) { System.out.print( " f2( long ) " ); }
    void f2( float x ) { System.out.print( " f2( float ) " ); }
    void f2( double x ) { System.out.print( " f2( double ) " ); }       
    void f3( short x ) { System.out.print( " f3( short ) " ); }
    void f3( int x ) { System.out.print( " f3( int ) " ); }
    void f3( long x ) { System.out.print( " f3( long ) " ); }
    void f3( float x ) { System.out.print( " f3( float ) " ); }
    void f3( double x ) { System.out.print( " f3( double ) " ); }
    void f4( int x ) { System.out.print( " f4( int ) " ); }
    void f4( long x ) { System.out.print( " long: " ); }
    void f4( float x ) { System.out.print( " f4( float ) " ); }
    void f4( double x ) { System.out.print( " f4( double ) " ); }
    void f5( long x ) { System.out.print( " f5( long: ): " ); }
    void f5( float x ) { System.out.print( " f5( float ) " ); }
    void f5( double x ) { System.out.print( " f5( double ) " ); }
    void f6( float x ) { System.out.print( " f6( float ) " ); }
    void f6( double x ) { System.out.print( " f6( double ) " ); }
    void f7( double x ) { System.out.print( " f7( double ) " ); }
    void testConstVal() {
        System.out.print( " 5: " );
        f1( 5 ); f2( 5 ); f3( 5 ); f4( 5 ); f5( 5 ); f6( 5 ); f7( 5 );
        System.out.println( );
    }
    void testChar() {
        char x ;
        System.out.print( " char: " );
        f1( 'x' ); f2( 'x' ); f3( 'x' ); f4( 'x' ); f5( 'x' ); f6( 'x' ); f7( 'x' );
        System.out.println( );   
    }
    void testByte() {
        byte x = 0;
        System.out.print( " byte: " );
        f1( x ); f2( x ); f3( x ); f4( x ); f5( x ); f6( x ); f7( x );
        System.out.println( );
    }
    void testShort() {
        short x = 0;
        System.out.print( " short: " );
        f1( x ); f2( x ); f3( x ); f4( x ); f5( x ); f6( x ); f7( x );
        System.out.println( );
    }
    void testInt() {
        int x = 0;
        System.out.print( " int: " );
        f1( x ); f2( x ); f3( x ); f4( x ); f5( x ); f6( x ); f7( x );
        System.out.println( );
    }
    void testLong() {
        long x = 0;
        System.out.print( " long: " );
        f1( x ); f2( x ); f3( x ); f4( x ); f5( x ); f6( x ); f7( x );
        System.out.println( );  
    }
    void testFloat() {
        float x = 0;
        System.out.print( " float: " );
        f1( x ); f2( x ); f3( x ); f4( x ); f5( x ); f6( x ); f7( x );
        System.out.println( );
    }
    void testDouble() {
        double x = 0;
        System.out.print( " double: " );
        f1( x ); f2( x ); f3( x ); f4( x ); f5( x ); f6( x ); f7( x );
        System.out.println( );
    }
    public static void main( String[] agrs ) {
        PrimitiveOverLoading79 p = new PrimitiveOverLoading79();
        p.testConstVal();
        p.testChar();
        p.testByte();
        p.testShort();
        p.testInt();
        p.testLong();
        p.testFloat();
        p.testDouble();
    }
}```
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/2562717-f7238139ad24a0f8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
***
當(dāng)傳入的實(shí)際參數(shù)大于重載方法聲明的形式參數(shù):
```java
public class Demotion81 {
    void f1( char x ) { System.out.println( " f1( char :  ) " ); }
    void f1( byte x ) { System.out.println( " f1( byte :  ) " ); }
    void f1( short x ) { System.out.println( " f1( short :  ) " ); }
    void f1( int x ) { System.out.println( " f1( int :  ) " ); }
    void f1( long x ) { System.out.println( " f1( long :  ) " ); }
    void f1( float x ) { System.out.println( " f1( float :  ) " ); }
    void f1( double x ) { System.out.println( " f1( double :  ) " ); }
    void f2( char x ) { System.out.println( " f2( char :  ) " ); }
    void f2( byte x ) { System.out.println( " f2( byte :  ) " ); }
    void f2( short x ) { System.out.println( " f2( short :  ) " ); }
    void f2( int x ) { System.out.println( " f2( int :  ) " ); }
    void f2( long x ) { System.out.println( " f2( long :  ) " ); }
    void f2( float x ) { System.out.println( " f2( float :  ) " ); }
    void f3( char x ) { System.out.println( " f3( char :  ) " ); } 
    void f3( short x ) { System.out.println( " f3( short :  ) " ); }
    void f3( long x ) { System.out.println( " f3( long :  ) " ); }
    void f3( int x ) { System.out.println( " f3( int :  ) " ); }
    void f3( byte x ) { System.out.println( " f3( byte :  ) " ); }
    void f4( char x ) { System.out.println( " f4( char ) " ); }
    void f4( int x ) { System.out.println( " f4( int :  ) " ); }
    void f4( byte x ) { System.out.println( " f4( byte :  ) " ); }
    void f4( float x ) { System.out.println( " f4( float :  ) " ); }
    void f5( char x ) { System.out.println( " f5( char :  ) " ); }
    void f5( byte x ) { System.out.println( " f5( byte :  ) " ); }
    void f5( short x ) { System.out.println( " f5( short :  ) " ); }
    void f6( char x ) { System.out.println( " f6( char :  ) " ); }
    void f6( byte x ) { System.out.println( " f6( byte :  ) " ); }
    void f7( char x ) { System.out.println( " f7( char :  ) " ); }
    void testDouble() {
        double x = 0;
        System.out.println( " double argument :  " );
        f1( x ); f2( ( float )x ); f3( ( long )x ); f4( ( int )x ); f5( ( short )x ); f6( ( byte )x ); f7( ( char )x );
/*  void test() {
        System.out.println( " test :  " );
        f1( 1 ); f2( 1 ); f3( 1 ); f4( 1 ); f5( 1 ); f6( 1 ); f7( 1 ); */
    } 
    public static void main( String[] args ) {
        Demotion81 p = new Demotion81();
        p.testDouble();
        //p.test();
    } 
}```
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/2562717-6a3f5de948b31df8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
在這里傳入的實(shí)際參數(shù)比較大,通過類型轉(zhuǎn)換執(zhí)行窄化轉(zhuǎn)換。如果不這樣做,編譯器就會(huì)報(bào)錯(cuò)。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • C++運(yùn)算符重載-上篇 本章內(nèi)容:1. 運(yùn)算符重載的概述2. 重載算術(shù)運(yùn)算符3. 重載按位運(yùn)算符和二元邏輯運(yùn)算符4...
    Haley_2013閱讀 2,385評(píng)論 0 51
  • C++運(yùn)算符重載-下篇 本章內(nèi)容:1. 運(yùn)算符重載的概述2. 重載算術(shù)運(yùn)算符3. 重載按位運(yùn)算符和二元邏輯運(yùn)算符4...
    Haley_2013閱讀 1,527評(píng)論 0 49
  • 第5章 引用類型(返回首頁) 本章內(nèi)容 使用對(duì)象 創(chuàng)建并操作數(shù)組 理解基本的JavaScript類型 使用基本類型...
    大學(xué)一百閱讀 3,675評(píng)論 0 4
  • 無意中看到了一段很有哲理的話: 當(dāng)孩子不麻煩你的時(shí)候, 可能已長大成人遠(yuǎn)離你了; 當(dāng)父母不麻煩你的時(shí)候, 可能這輩...
    溫暖的陽光燦爛閱讀 620評(píng)論 0 1
  • 先簡單說一下,這個(gè)題目雖然有點(diǎn)標(biāo)題黨,但是絕對(duì)不是危言聳聽。 換成這種類似的題目是想測試一下,自己的一個(gè)關(guān)于文章標(biāo)...
    蕭牧寒閱讀 356評(píng)論 0 0

友情鏈接更多精彩內(nèi)容