你的位置:首页 > 软件开发 > Java > 方法重载,为什么不能根据返回类型来区分?

方法重载,为什么不能根据返回类型来区分?

发布时间:2015-07-24 11:00:07
详见:四一、方法重载简述任何程序设计语言都具备一项重要特性就是对名字的运用。当创建一个对象时,也就给此对象分配到的存储空间取一个名字。所谓方法,则是给某个动作取的名字。通过这个名字,你可以引用所有的对象和方法。名字起的好可以使系统更易于理解和修改。在日常生活中,相同的词可以表达多 ...

详见:四

一、方法重载简述

任何程序设计语言都具备一项重要特性就是对名字的运用。当创建一个对象时,也就给此对象分配到的存储空间取一个名字。

所谓方法,则是给某个动作取的名字。通过这个名字,你可以引用所有的对象和方法。名字起的好可以使系统更易于理解和修改。

在日常生活中,相同的词可以表达多种不同的含义——它们被“重载”了。特别是含义之间的差别很小时,这种方式十分有用。比起“以洗衬衫的方式洗衬衫”、“以洗狗的方式洗狗”、“以洗车的方式洗车”,“洗洗衬衫”、“洗洗狗”、“洗洗车”显得更加简洁和方便。

   wash(Shirt shirt); wash(Dog dog); wash(Car car);

大多数程序设计语言(尤其是C)要求为每个方法都提供一个独一无二的标识符。所以绝不能用名为print()的函数显示一个整数之后,有用print()显示浮点数。即不支持重载,每个函数都要有唯一的名称。

Java和C++支持重载(例如:构造方法重载,通过不同的构造方法,用多种方式创建一个对象)。

 

二、区分重载方法

规则很简单:每个重载方法都必须有一个独一无二的参数类型列表

  1、参数个数

  2、参数类型

  3、参数顺序(一般不用,容易造成维护困难)

 

三、涉及基本类型的重载

1、当实参类型小于形参类型,实际数据类型就会被提升。

  注意:char类型略有不同,如果无法找到恰好接受char参数的方法,就会把char直接提升至int型。

示例代码:

方法重载,为什么不能根据返回类型来区分?方法重载,为什么不能根据返回类型来区分?
 1 package com.mianshi.easy; 2  3 public class Overload { 4    5   void f1(char x){System.out.print("f1(char)"+"、");}; 6   void f1(byte x){System.out.print("f1(byte)"+"、");}; 7   void f1(int x){System.out.print("f1(int)"+"、");}; 8   void f1(long x){System.out.print("f1(long)"+"、");}; 9   void f1(float x){System.out.print("f1(float)"+"、");}; 10   void f1(short x){System.out.print("f1(short)"+"、");}; 11   void f1(double x){System.out.print("f1(double)"+"、");}; 12    13   void f2(byte x){System.out.print("f2(byte)"+"、");}; 14   void f2(int x){System.out.print("f2(int)"+"、");}; 15   void f2(long x){System.out.print("f2(long)"+"、");}; 16   void f2(float x){System.out.print("f2(float)"+"、");}; 17   void f2(short x){System.out.print("f2(short)"+"、");}; 18   void f2(double x){System.out.print("f2(double)"+"、");}; 19    20   void f3(int x){System.out.print("f3(int)"+"、");}; 21   void f3(long x){System.out.print("f3(long)"+"、");}; 22   void f3(float x){System.out.print("f3(float)"+"、");}; 23   void f3(short x){System.out.print("f3(short)"+"、");}; 24   void f3(double x){System.out.print("f3(double)"+"、");}; 25    26   void f4(int x){System.out.print("f4(int)"+"、");}; 27   void f4(long x){System.out.print("f4(long)"+"、");}; 28   void f4(float x){System.out.print("f4(float)"+"、");}; 29   void f4(double x){System.out.print("f4(double)"+"、");}; 30    31   void f5(long x){System.out.print("f5(long)"+"、");}; 32   void f5(float x){System.out.print("f5(float)"+"、");}; 33   void f5(double x){System.out.print("f5(double)"+"、");}; 34    35   void f6(float x){System.out.print("f6(float)"+"、");}; 36   void f6(double x){System.out.print("f6(double)"+"、");}; 37    38   void f7(double x){System.out.print("f7(double)"+"、");}; 39    40   void testConstVal(){ 41     System.out.print("5:"+"   "); 42     f1(5);f2(5);f3(5);f4(5);f5(5);f6(5);f7(5); 43   } 44    45   void testChar(){ 46     char x = 'x'; 47     System.out.print("char:"+"  "); 48     f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); 49   } 50    51   void testByte(){ 52     byte x = 0; 53     System.out.print("byte:"+"  "); 54     f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); 55   } 56    57   void testShort(){ 58     byte x = 0; 59     System.out.print("short:"+" "); 60     f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); 61   } 62    63   void testInt(){ 64     int x = 0; 65     System.out.print("int:"+"  "); 66     f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); 67   } 68   void testLong(){ 69     long x = 0; 70     System.out.print("long:"+"  "); 71     f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); 72   } 73    74   void testFloat(){ 75     float x = 0; 76     System.out.print("float:"+" "); 77     f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); 78   } 79    80   void testDouble(){ 81     double x = 0; 82     System.out.print("double:"+" "); 83     f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); 84   } 85    86   public static void main(String[] args) { 87      88     Overload o = new Overload(); 89      90     //按顺序一一解注释下面代码,观察结果 91     o.testChar(); 92     /*o.testByte(); 93     o.testConstVal(); 94     o.testInt(); 95     o.testShort(); 96     o.testLong(); 97     o.testFloat(); 98     o.testDouble();*/ 99   }100 }

原标题:方法重载,为什么不能根据返回类型来区分?

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录