方法的重载
当创建一个对象时,就是给此对象分配到的存储空间取了一个名字。使用名字可以引用所有的对象和方法。方法则是给某个动作取得名字。
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();
}
}
}```
这里用到了相同名字的两个构造器,这就叫重载。
用到了两个构造器:一个默认构造器,一个取字符串作为形式参数——该字符串表示初始化对象所需的文件名称。都是构造器,有相同的名字。
函数重载的最重要目的是因为他们都在做同一件事情,虽然输入不同,但是做的事情相同
***
###区分重载方法
每个重载方法都有自己独一无二的**参数类型列表**。
```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 " );
}
}```
甚至参数顺序不同也可以区分开。
***
###基本类型的重载
基本类型从“较小”的类型自动提升到一个“较大”的类型,此过程一旦牵涉到重载,可能会造成一些混淆。
```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)
***
当传入的实际参数大于重载方法声明的形式参数:
```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)
在这里传入的实际参数比较大,通过类型转换执行窄化转换。如果不这样做,编译器就会报错。