当前位置: 首页 > Thinking in Java > 正文

第5章–初始化与清理_重载区分,基本 类型重载

第5章–初始化与清理_重载区分,基本类型重载

 

1. 不能以方法的返回值来区分方法

 

2. 当方法参数为基本类型时,重载会产生混乱,

 

   因为如果调用的函数的参数类型没有适合的,基本类型会从一个较小类型自动转换为较大类型,然后再次寻找适合的方法

   比如,调用int x = 100; f(x);假如 f(int x);这样的函数没有,但f(long x) 有这样的函数存在,

   则int x = 100; f(x);实际上调用的是f(long x);//将整形的x先自动提升为long了,然后调用f(long x);

   另,假如 char x = ‘x’,调用f(x),没有f(char x),但存在f(byte x),f(short x),f(int x),f(long x)

   则,调用char x = ‘x’;f(x)时,实际上调用的是f(int x),即对于char类型的参数,重载调用,没有找到具有适当参数的函数时,

   会直接将char提升至int(忽略char 和 int 之间的 byte和short),然后在寻找适合的方法(这里寻找f(int x),且寻找到了,则调用),

   假如没有找到f(int x),则将int x类型再次提升为long,然后调用f(long x)

 

例如:

public class TestOverload
{
  //fChar()这样格式的函数,参数是从char开始,fByte参数从Byte开始,依次类推
  //比如 fDouble()就只有fDouble(double x)这个参数,
  //这样假如调用char x = 'x';fDouble(x);这里x就需要从char提升至int 然后再提升至Double
  
  //testchar
  void fChar(char c) 
  {
    System.out.println("fChar(char c)  char: " + c);
  }
  
  void fChar(byte c) 
  {
    System.out.println("fChar(byte c) byte: " + c);
  }
  
  void fChar(short c) 
  {
    System.out.println("fChar(short c) short: " + c);
  }
  
  void fChar(int c) 
  {
    System.out.println("fChar(int c) int: " + c);
  }
  
  void fChar(long c) 
  {
    System.out.println("fChar(long c) long: " + c);
  }
  
  void fChar(float c) 
  {
    System.out.println("fChar(float c)  float: " + c);
  }
  
  void fChar(double c) 
  {
    System.out.println("fChar(double c) double: " + c);
  }
  
  //fbyte
  void fByte(byte c) 
  {
    System.out.println("fByte(byte c) byte: " + c);
  }
  
  void fByte(short c) 
  {
    System.out.println("fByte(short c)  short: " + c);
  }
  
  void fByte(int c) 
  {
    System.out.println("fByte(int c) int: " + c);
  }
  
  void fByte(long c) 
  {
    System.out.println("fByte(long c) long: " + c);
  }
  
  void fByte(float c) 
  {
    System.out.println("fByte(float c) float: " + c);
  }
  
  void fByte(double c) 
  {
    System.out.println("fByte(double c) double: " + c);
  }
  
  //testShort
  void fShort(short c) 
  {
    System.out.println("fShort(short c) short: " + c);
  }
  
  void fShort(int c) 
  {
    System.out.println("fShort(int c) int: " + c);
  }
  
  void fShort(long c) 
  {
    System.out.println("fShort(long c) long: " + c);
  }
  
  void fShort(float c) 
  {
    System.out.println("void fShort(float c)  float: " + c);
  }
  
  void fShort(double c) 
  {
    System.out.println("fShort(double c) double: " + c);
  }
  
  //testint
  void fInt(int c) 
  {
    System.out.println("fInt(int c) int: " + c);
  }
  
  void fInt(long c) 
  {
    System.out.println("fInt(long c) long: " + c);
  }
  
  void fInt(float c) 
  {
    System.out.println("fInt(float c)  float: " + c);
  }
  
  void fInt(double c) 
  {
    System.out.println("fInt(double c) double: " + c);
  }
  
  //testlong
  void fLong(long x)
  {
    System.out.println("fLong(long x) long: " + x);
  }
  
  void fLong(float x)
  {
    System.out.println("fLong(long x) float: " + x);
  }
  
  void fLong(double x)
  {
    System.out.println("fLong(long x) double: " + x);
  }
  
  //testFloat
  void fFloat(float c) 
  {
    System.out.println("fFloat(float c) float: " + c);
  }
  
  void fFloat(double c) 
  {
    System.out.println("fFloat(double c)  double: " + c);
  }
  
  //testDouble
  void fDouble(double c) 
  {
    System.out.println("fDouble(double c)  double: " + c);
  }
  
  void testChar()
  {
    char x = 'x';
    System.out.println("当参数为:char x='x'");
    fChar(x);fByte(x);fShort(x);fInt(x);fLong(x);fFloat(x);fDouble(x);
    System.out.println("--------------------------------");
    /**
     * 输出为:
     *  当参数为:char x='x'
        fChar(char c)  char: x
        fByte(int c) int: 120     //调用fByte,存在fByte(byte x)的,但是没有调用,直接将char提升至int,然后调用fByte(int c)了
        fShort(int c) int: 120
        fInt(int c) int: 120
        fLong(long x) long: 120  //先将char提升为int,但没有fLong(int x)函数,只能再提升至long了,然后调用fLong(long x)
        fFloat(float c) float: 120.0
        fDouble(double c)  double: 120.0
     */
  }
  
  void testByte() 
  {
    byte x = 99;
    System.out.println("当参数为:byte x = 99");
    fChar(x);fByte(x);fShort(x);fInt(x);fLong(x);fFloat(x);fDouble(x);
    System.out.println("--------------------------------");
  }
  
  void testShort() 
  {
    short x = 12345;
    System.out.println("当参数为:short x = 12345");
    fChar(x);fByte(x);fShort(x);fInt(x);fLong(x);fFloat(x);fDouble(x);
    System.out.println("--------------------------------");
  }
  
  void testInt() 
  {
    int x = 1000;
    System.out.println("当参数为:int x = 1000");
    fChar(x);fByte(x);fShort(x);fInt(x);fLong(x);fFloat(x);fDouble(x);
    System.out.println("--------------------------------");
  }
  
  void testLong() 
  {
    long x = 9999L;
    System.out.println("当参数为:long x = 9999L");
    fChar(x);fByte(x);fShort(x);fInt(x);fLong(x);fFloat(x);fDouble(x);
    System.out.println("--------------------------------");
  }
  
  void testFloat() 
  {
    float x = 99.99F;
    System.out.println("当参数为:float x = 99.99F");
    fChar(x);fByte(x);fShort(x);fInt(x);fLong(x);fFloat(x);fDouble(x);
    System.out.println("--------------------------------");
  }
  
  void testDouble() 
  {
    double x = 99.99D;
    System.out.println("当参数为:double x = 99.99D");
    fChar(x);fByte(x);fShort(x);fInt(x);fLong(x);fFloat(x);fDouble(x);
    System.out.println("--------------------------------");
  }
  
  public static void main(String[] args)
  {
    TestOverload t = new TestOverload();
    t.testChar();
    t.testByte();
    t.testShort();
    t.testInt();
    t.testLong();
    t.testFloat();
    t.testDouble();
    
  }
}

以上代码输出:

当参数为:char x=’x’

fChar(char c)  char: x

fByte(int c) int: 120

fShort(int c) int: 120

fInt(int c) int: 120

fLong(long x) long: 120

fFloat(float c) float: 120.0

fDouble(double c)  double: 120.0

——————————–

当参数为:byte x = 99

fChar(byte c) byte: 99

fByte(byte c) byte: 99

fShort(short c) short: 99

fInt(int c) int: 99

fLong(long x) long: 99

fFloat(float c) float: 99.0

fDouble(double c)  double: 99.0

——————————–

当参数为:short x = 12345

fChar(short c) short: 12345

fByte(short c)  short: 12345

fShort(short c) short: 12345

fInt(int c) int: 12345

fLong(long x) long: 12345

fFloat(float c) float: 12345.0

fDouble(double c)  double: 12345.0

——————————–

当参数为:int x = 1000

fChar(int c) int: 1000

fByte(int c) int: 1000

fShort(int c) int: 1000

fInt(int c) int: 1000

fLong(long x) long: 1000

fFloat(float c) float: 1000.0

fDouble(double c)  double: 1000.0

——————————–

当参数为:long x = 9999L

fChar(long c) long: 9999

fByte(long c) long: 9999

fShort(long c) long: 9999

fInt(long c) long: 9999

fLong(long x) long: 9999

fFloat(float c) float: 9999.0

fDouble(double c)  double: 9999.0

——————————–

当参数为:float x = 99.99F

fChar(float c)  float: 99.99

fByte(float c) float: 99.99

void fShort(float c)  float: 99.99

fInt(float c)  float: 99.99

fLong(long x) float: 99.99

fFloat(float c) float: 99.99

fDouble(double c)  double: 99.98999786376953

——————————–

当参数为:double x = 99.99D

fChar(double c) double: 99.99

fByte(double c) double: 99.99

fShort(double c) double: 99.99

fInt(double c) double: 99.99

fLong(long x) double: 99.99

fFloat(double c)  double: 99.99

fDouble(double c)  double: 99.99

——————————–

赞 赏

   微信赞赏  支付宝赞赏


本文固定链接: https://www.jack-yin.com/coding/thinking-in-java/2395.html | 边城网事

该日志由 边城网事 于2013年06月08日发表在 Thinking in Java 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。
原创文章转载请注明: 第5章–初始化与清理_重载区分,基本 类型重载 | 边城网事
关键字: ,

第5章–初始化与清理_重载区分,基本 类型重载 暂无评论

发表评论

快捷键:Ctrl+Enter