为什么接口类型被认为在重载时是根本不同的?

huangapple go评论74阅读模式
英文:

Why are interface types considered to be radically different for overloading?

问题

在《Effective Java》一书中,Joshua Bloch提到了方法重载的问题:

> 导出具有相同数量参数的多个重载方法,只要始终清楚哪个重载方法将适用于给定的实际参数集,通常不会使程序员感到困惑。这种情况发生在每对重载方法中至少有一个对应的形式参数在两个重载方法中具有“根本不同”的类型时。如果无法将任何非空表达式转换为这两种类型,则这两种类型是根本不同的。

然而,随后它继续说道:

> 数组类型和除了Object之外的类类型都是根本不同的。此外,数组类型和除了Serializable和Cloneable之外的接口类型也是根本不同的。

我的问题是,为什么接口类型被认为是根本不同的,尽管Java允许多个接口实现?

例如,假设我有一个类Foo,它实现了接口BarableBazable。如果我有一对重载方法,一个接受Barable作为参数,另一个接受Bazable,为什么这不会产生歧义呢?例如,以下代码由于重载歧义而无法编译:

interface Bazable {
  public void baz();
}

interface Barable {
  public void bar();
}

class Foo implements Barable, Bazable {
  public Foo() {

  }
  public void baz() {
    System.out.println("Can baz");
  }

  public void bar(){
    System.out.println("Can bar");
  }
}

class Main {
  public static void main(String[] args) {
    Foo f = new Foo();
    test(f);

  }

  public static void test(Barable t) {
    System.out.println("Using barable overload");
  }

  public static void test(Bazable t) {
    System.out.println("Using bazable overload");
  }
  
}

(请注意:这只是前面提到的问题的一部分,没有额外的内容或回答。)

英文:

In Effective Java by Joshua Bloch, it says, in reference to overloading:

> Exporting multiple overloadings with the same number of parameters is unlikely to confuse programmers if it is always clear which overloading will apply to any given set of actual parameters. This is the case when at least one corresponding formal parameter in each pair of overloadings has a “radically different” type in the two overloadings. Two types are radically different if it is clearly impossible to cast any non-null expression to both types.

However, then it goes on to say:

> Array types and class types other than Object are radically different. Also, array types and interface types other than Serializable and Cloneable are radically different.

My question is why are interface types considered radically different, despite Java allowing for multiple interface implementations?

For example, say I have class Foo that implements interface Barable and Bazable. If I had a pair of overloaded methods, one accepting Barable as a parameter, and the other accepting Bazable, why wouldn't that be ambiguous? The following, for example, won't compile due to ambiguous overloading:

interface Bazable {
  public void baz();
}

interface Barable {
  public void bar();
}

class Foo implements Barable, Bazable {
  public Foo() {

  }
  public void baz() {
    System.out.println("Can baz");
  }

  public void bar(){
    System.out.println("Can bar");
  }
}

class Main {
  public static void main(String[] args) {
    Foo f = new Foo();
    test(f);

  }

  public static void test(Barable t) {
    System.out.println("Using barable overload");
  }

  public static void test(Bazable t) {
    System.out.println("Using bazable overload");
  }
  
}

答案1

得分: 4

这句话的意思是,数组类型和除了Serializable和Cloneable之外的接口类型有根本不同。

...的意思是接口类型与数组类型有根本不同,而不是接口类型彼此之间有根本不同。Serializable和Cloneable被排除在外的原因是数组类型实现了这两个接口。

英文:

The meaning of this statement,
> array types and interface types other than Serializable and Cloneable are radically different.

...is that interface types are radically different from array types, not that interface types are radically different from each other. The reason Serializable and Cloneable are excluded is that array types implement those two interfaces.

huangapple
  • 本文由 发表于 2020年8月9日 05:22:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63320352.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定