方法的重載
當(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();
}
}```

***
當(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();
}
}```

在這里傳入的實(shí)際參數(shù)比較大,通過類型轉(zhuǎn)換執(zhí)行窄化轉(zhuǎn)換搂擦。如果不這樣做稳诚,編譯器就會(huì)報(bào)錯(cuò)。