Day14-45.Method overloading

方法重载

任何程序语言都具备的一项重要特性就是对名字的运用。

当创建一个对象时,也就给此对象分配到的存储空间取了一个名字。

所谓方法则是给某个动作取的名字。

通过使用名字,你可以引用所有的对象和方法。

名字起的好可以使系统更易于理解和修改。

就好比写散文—---目的是让读者易于理解。

将人类语言中存在细微差别的概念“映射”到程序设计语言中时,问题随着产生。

在日常生活中,相同的词可以表达多种不同的含义——它们被重载了。

特别是含义之间的差别很小时,这种方式十分有用。

你可以说清洗衬衫,清洗车,清洗狗。

但如果硬要这样说就显得很愚蠢:”以洗衬衫的方式洗衬衫“”以洗车的方式洗车“”以洗狗的方式洗狗“。

这是因为听众根本不需要对所执行的动作做出明确的区分。

大多数人类语言具有很强的冗余性,即使漏掉几个词,仍然可以推断出含义。

不需要对每个概念都使用不同的词汇——从具体的语境中就可以推断出含义。

大多数程序设计语言尤其是C要求为每个方法(在这些语言中经常称为函数)都提供一个独一无二的标识符。

所以决不能用名为print()函数显示了整数之后,又用一个名为print的函数显示浮点数——每个函数都有唯一的名称。

在Java和C++里,构造器是强制重载方法名的另一个原因。

既然构造器的名字已经由类名所决定,就只能有一个构造器名。

那么想要用多种方式创建一个对象该怎么办呢?

假设你要创建一个类,既可以用标准方式进行初始化,也可以从文件里读取信息来初始化。

这就需要两个构造器:一个默认构造器,另一个取字符串作为形式参数——该字符串表示初始化对象所需的文件名称。

由于都是构造器,所以它们必须有相同的名字,即类名。

为了让方法名相同而形式参数不同的构造器同时存在,必须用到方法重载。

同时,尽管方法重载是构造器所必须的,但他亦可应用于其他方法,且用法同样方便。

下面这个例子同时示范了重载的构造器和重载的方法:

//: initialization/Overloading.java
// Demonstration of both constructor
// and ordinary method overloading.
import static net.mindview.util.Print.*;

class Tree {
  int height;
  Tree() {
    print("Planting a seedling");
    height = 0;
  }
  Tree(int initialHeight) {
    height = initialHeight;
    print("Creating new Tree that is " +
      height + " feet tall");
  }	
  void info() {
    print("Tree is " + height + " feet tall");
  }
  void info(String s) {
    print(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("overloaded method");
    }
    // Overloaded constructor:
    new Tree();
  }	
} /* Output:
Creating new Tree that is 0 feet tall
Tree is 0 feet tall
overloaded method: Tree is 0 feet tall
Creating new Tree that is 1 feet tall
Tree is 1 feet tall
overloaded method: Tree is 1 feet tall
Creating new Tree that is 2 feet tall
Tree is 2 feet tall
overloaded method: Tree is 2 feet tall
Creating new Tree that is 3 feet tall
Tree is 3 feet tall
overloaded method: Tree is 3 feet tall
Creating new Tree that is 4 feet tall
Tree is 4 feet tall
overloaded method: Tree is 4 feet tall
Planting a seedling
*///:~



上面中两个print()方法虽然生命了相同的参数,但是顺序不同,因此得以区分。

Overloading with primitives 

涉及基本类型的重载

基本类型能从一个较小的类型自动提升至一个较大的类型,此过程一旦牵涉到重载,可能会造成一些混淆。

以下例子说明了将基本类型传递给重载方法时发生的情况:

//: initialization/PrimitiveOverloading.java
// Promotion of primitives and overloading.
import static net.mindview.util.Print.*;

public class PrimitiveOverloading {
  void f1(char x) { printnb("f1(char) "); }
  void f1(byte x) { printnb("f1(byte) "); }
  void f1(short x) { printnb("f1(short) "); }
  void f1(int x) { printnb("f1(int) "); }
  void f1(long x) { printnb("f1(long) "); }
  void f1(float x) { printnb("f1(float) "); }
  void f1(double x) { printnb("f1(double) "); }

  void f2(byte x) { printnb("f2(byte) "); }
  void f2(short x) { printnb("f2(short) "); }
  void f2(int x) { printnb("f2(int) "); }
  void f2(long x) { printnb("f2(long) "); }
  void f2(float x) { printnb("f2(float) "); }
  void f2(double x) { printnb("f2(double) "); }

  void f3(short x) { printnb("f3(short) "); }
  void f3(int x) { printnb("f3(int) "); }
  void f3(long x) { printnb("f3(long) "); }
  void f3(float x) { printnb("f3(float) "); }
  void f3(double x) { printnb("f3(double) "); }

  void f4(int x) { printnb("f4(int) "); }
  void f4(long x) { printnb("f4(long) "); }
  void f4(float x) { printnb("f4(float) "); }
  void f4(double x) { printnb("f4(double) "); }

  void f5(long x) { printnb("f5(long) "); }
  void f5(float x) { printnb("f5(float) "); }
  void f5(double x) { printnb("f5(double) "); }

  void f6(float x) { printnb("f6(float) "); }
  void f6(double x) { printnb("f6(double) "); }

  void f7(double x) { printnb("f7(double) "); }

  void testConstVal() {
    printnb("5: ");
    f1(5);f2(5);f3(5);f4(5);f5(5);f6(5);f7(5); print();
  }
  void testChar() {
    char x = 'x';
    printnb("char: ");
    f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); print();
  }
  void testByte() {
    byte x = 0;
    printnb("byte: ");
    f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); print();
  }
  void testShort() {
    short x = 0;
    printnb("short: ");
    f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); print();
  }
  void testInt() {
    int x = 0;
    printnb("int: ");
    f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); print();
  }
  void testLong() {
    long x = 0;
    printnb("long: ");
    f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); print();
  }
  void testFloat() {
    float x = 0;
    printnb("float: ");
    f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); print();
  }
  void testDouble() {
    double x = 0;
    printnb("double: ");
    f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); print();
  }
  public static void main(String[] args) {
    PrimitiveOverloading p =
      new PrimitiveOverloading();
    p.testConstVal();
    p.testChar();
    p.testByte();
    p.testShort();
    p.testInt();
    p.testLong();
    p.testFloat();
    p.testDouble();
  }
} /* Output:
5: f1(int) f2(int) f3(int) f4(int) f5(long) f6(float) f7(double)
char: f1(char) f2(int) f3(int) f4(int) f5(long) f6(float) f7(double)
byte: f1(byte) f2(byte) f3(short) f4(int) f5(long) f6(float) f7(double)
short: f1(short) f2(short) f3(short) f4(int) f5(long) f6(float) f7(double)
int: f1(int) f2(int) f3(int) f4(int) f5(long) f6(float) f7(double)
long: f1(long) f2(long) f3(long) f4(long) f5(long) f6(float) f7(double)
float: f1(float) f2(float) f3(float) f4(float) f5(float) f6(float) f7(double)
double: f1(double) f2(double) f3(double) f4(double) f5(double) f6(double) f7(double)
*///:~
创建tree对象的时候,既可以不含参数,也可以用树的高度当参数,
前者表示一颗树苗,后者表示已有一定高度的数木。
要支持这种创建方式,得有一个默认构造器和一个采用现有高度作为参数的构造器,
或许你还想通过多种方式调用info()方法
例如,你想显示额外信息,可以用info(String)方法;没有的话就用info。
要是对明显相同的概念使用了不同的名字,那一定会让人很纳闷。
好在有了方法重载,可以为两者使用相同的名字。
Distinguishing overloaded methods
 区分重载方法
要是几个方法有相同的名字,Java如何才能知道你指的是哪一个呢?
其实规则很简单,每个重载的方法都必须有一个独一无二的参数类型列表。
稍加思考,就会觉得这是合理的。
毕竟对于名字相同的方法,除了参数类型的差异以外,还有什么办法能把它们区别开呢?
甚至参数顺序的不同也足以区分两个方法。不过,一般情况下别这么做,因为这会使代码难以维护:


//: initialization/OverloadingOrder.java
// Overloading based on the order of the arguments.
import static net.mindview.util.Print.*;
public class OverloadingOrder {
  static void f(String s, int i) {
    print("String: " + s + ", int: " + i);
  }
  static void f(int i, String s) {
    print("int: " + i + ", String: " + s);
  }
  public static void main(String[] args) {
    f("String first", 11);
    f(99, "Int first");
  }
} /* Output:
String: String first, int: 11
int: 99, String: Int first
*///:~


你会发现常数值5倍当做int处理,所以如果有某个重载方法接受int型参数,它就会被调用。

至于其他情况,如果传入的数据类型(实际参数类型)小于方法中声明的形式参数类型,实际数据类型就会被提升。

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

如果传入的实际参数大于重载方法声明的形式参数,会出现什么情况呢?

修改上述程序,就能得到答案:

//: initialization/Demotion.java
// Demotion of primitives and overloading.
import static net.mindview.util.Print.*;

public class Demotion {
  void f1(char x) { print("f1(char)"); }
  void f1(byte x) { print("f1(byte)"); }
  void f1(short x) { print("f1(short)"); }
  void f1(int x) { print("f1(int)"); }
  void f1(long x) { print("f1(long)"); }
  void f1(float x) { print("f1(float)"); }
  void f1(double x) { print("f1(double)"); }

  void f2(char x) { print("f2(char)"); }
  void f2(byte x) { print("f2(byte)"); }
  void f2(short x) { print("f2(short)"); }
  void f2(int x) { print("f2(int)"); }
  void f2(long x) { print("f2(long)"); }
  void f2(float x) { print("f2(float)"); }

  void f3(char x) { print("f3(char)"); }
  void f3(byte x) { print("f3(byte)"); }
  void f3(short x) { print("f3(short)"); }
  void f3(int x) { print("f3(int)"); }
  void f3(long x) { print("f3(long)"); }

  void f4(char x) { print("f4(char)"); }
  void f4(byte x) { print("f4(byte)"); }
  void f4(short x) { print("f4(short)"); }
  void f4(int x) { print("f4(int)"); }

  void f5(char x) { print("f5(char)"); }
  void f5(byte x) { print("f5(byte)"); }
  void f5(short x) { print("f5(short)"); }

  void f6(char x) { print("f6(char)"); }
  void f6(byte x) { print("f6(byte)"); }

  void f7(char x) { print("f7(char)"); }

  void testDouble() {
    double x = 0;
    print("double argument:");
    f1(x);f2((float)x);f3((long)x);f4((int)x);
    f5((short)x);f6((byte)x);f7((char)x);
  }
  public static void main(String[] args) {
    Demotion p = new Demotion();
    p.testDouble();
  }
} /* Output:
double argument:
f1(double)
f2(float)
f3(long)
f4(int)
f5(short)
f6(byte)
f7(char)
*///:~



在这里,方法接受较小的基本类型作为参数。

如果传入的实际参数较大,就得通过类型转换来执行窄化转换。

如果不这样做,编译器就会报错。


Overloading on return values 


以返回值区分重载方法

读者可能回想,在区分重载方法的时候,为什么只能以类名和方法的形参列表作为标准呢?

能否考虑用方法的返回值来区分呢?

比如下面两个方法,虽然他们有同样的名字和形式参数,但却很容易区分他们:

void f(){}

int f(){return 1;}

只要编译器可以根据语境明确判断出语义,比如在int x=f()中,那么的确可以据此区分重载方法。

不过,有时你并不关心方法的返回值,你想要的是方法调用的其他效果(这常被成为“为了副作用而调用”),这时你可能会调用方法而忽略其返回值。

所以,如果像下面这样调用方法:

f();

此时Java如何才能判断该调用哪一个f()呢?

别人该如何理解这种代码呢?

因此,根据方法的返回值来区分重载方法是行不通的。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值