重载

作者: 海豚的小小海 | 来源:发表于2016-07-29 20:19 被阅读0次

方法的重载

当创建一个对象时,就是给此对象分配到的存储空间取了一个名字。使用名字可以引用所有的对象和方法。方法则是给某个动作取得名字。

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](https://img.haomeiwen.com/i2562717/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](https://img.haomeiwen.com/i2562717/6a3f5de948b31df8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
在这里传入的实际参数比较大,通过类型转换执行窄化转换。如果不这样做,编译器就会报错。

相关文章

  • PHP编程开发技巧:PHP中重载如何实现

    PHP编程开发技巧:PHP中重载如何实现,重载分为属性重载和方法重载,在PHP中的重载和在其它编程语言的重载不一样...

  • C++操作符重载

    重载操作符的限制 可以重载的操作符 不能重载的算符 操作符重载的语法形式 重载赋值操作符 重载+-*/运算操作符操...

  • Java重载与重写

    简言 重写和重载都是面向对象多态的一种表现,重载是编译时多态,重载是运行时多态. 重载 重载(Overloadin...

  • C++基础-(重载)

    C++基础 重载 哪些运算符可以被重载:::,.,->,*,?:不能被重载 重载操作符的标志(operator) ...

  • C++ 部分运算符重载

    可重载的运算符 不可重载的运算符和符号 重载运算符为类的成员函数 重载运算符为友元函数 重载赋值运算符 重载流插入...

  • 2019-07-11 运算符的重载

    运算符的重载 重载运算符 (“::”,“.*”,“.”,“?:”)不能被重载。 重载运算符时,不能改变其本质,如不...

  • Cocos2dx之C++基础(四)

    函数重载函数不以返回值来区分重载函数函数不以参数名来区分重载函数使用重载函数的时候不要引起二义性结构函数也可以重载...

  • C#多态的表现-方法重载,方法重写以及密封类的特点

    方法重载 方法重载:构造函数就属于特殊的方法重载 方法重载:是指同一个类中相同方法的不同行为 方法重载的特点: 1...

  • C语言到C++(4) - 重载

    C++中新增了重载功能,重载分为两部分,函数重载和运算符重载。 1. 函数重载 在C语言编程中,我们经常遇到一组函...

  • C++ 操作符重载

    重载算数操作符 重载操作符[] 重载关系操作符 重载类型转换操作符 重载输入输出操作符 引例 用一个类Fracti...

网友评论

      本文标题:重载

      本文链接:https://www.haomeiwen.com/subject/wsbzjttx.html