简体   繁体   English

“默认”(界面)如何工作?

[英]How does “default” (interface) work?

Why is this program giving the output "Class A"? 为什么这个程序输出“A类”?

abstract class A {
      public void abc() {
        System.out.println("Class A");
      }
    }         
    interface B {
      default void abc() {
        System.out.println("Interface B");
      }
    }
    public class Test extends A implements B {
      public static void main(String[] args) {
        Test t = new Test();
        t.abc();
      }
    }

Can anyone explain wh? 任何人都可以解释wh?

Though the both abstract class and interface have the same method, abstract class method have the high preference while choosing at run time. 虽然抽象类和接口都具有相同的方法,但抽象类方法在运行时选择时具有较高的偏好。

Here is the JLS on Method invocation principles 这是JLS on Method invocation principlesJLS on Method invocation principles

It is possible that no method is the most specific, because there are two or more methods that are maximally specific. 可能没有方法是最具体的,因为有两种或更多种方法是最具体的。 In this case: 在这种情况下:

If all the maximally specific methods have override-equivalent signatures (§8.4.2), then: 如果所有最大特定方法都具有覆盖等效签名(第8.4.2节),则:

If exactly one of the maximally specific methods is concrete (that is, non-abstract or default), it is the most specific method. 如果具体的最大特定方法之一是具体的(即非抽象或默认),则它是最具体的方法。

Otherwise, if all the maximally specific methods are abstract or default, and the signatures of all of the maximally specific methods have the same erasure (§4.6), then the most specific method is chosen arbitrarily among the subset of the maximally specific methods that have the most specific return type. 否则,如果所有最大特定方法都是抽象的或默认的,并且所有最大特定方法的签名都具有相同的擦除(§4.6),那么在具有最大特定方法的子集中任意选择最具体的方法。最具体的回报类型。

In this case, the most specific method is considered to be abstract . 在这种情况下,最具体的方法被认为是抽象的 Also, the most specific method is considered to throw a checked exception if and only if that exception or its erasure is declared in the throws clauses of each of the maximally specific methods. 此外,当且仅当在每个最大特定方法的throws子句中声明该异常或其擦除时,才考虑将最具体的方法抛出一个已检查的异常。

Otherwise, the method invocation is ambiguous, and a compile-time error occurs. 否则,方法调用不明确,并发生编译时错误。

When you declare that you are implementing an interface, it is the same as saying:"I will make an implementation for all the methods declared in this interface". 当你声明你正在实现一个接口时,它就像说:“我将为这个接口中声明的所有方法做一个实现”。 When you extends an abstract class you say:"I will implement all the abstract methods in this class and inherits all the non-abstract methods". 当您扩展一个抽象类时,您会说:“我将在此类中实现所有抽象方法并继承所有非抽象方法”。

So, you inherited the defined method to satisfy the interface' needs, the inheritance override the interface' default. 因此,您继承了已定义的方法以满足接口的需求,继承覆盖接口的默认值。

Edit: I have learnt something new today. 编辑:我今天学到了新东西。 Abstract methods have higher preference while choosing at run time. 抽象方法在运行时选择具有较高的偏好。

An interface is a collection of abstract methods. 接口是抽象方法的集合。 So the 所以

System.out.println("Interface B");

statement is not inherited. 声明不是继承的。 The

System.out.println("Class A");

is what is inherited in the instance of Test t, as it is abstract sand has higher preference. 是在Test t的实例中继承的,因为它是抽象的沙子具有更高的偏好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM