从间接继承的界面中选择默认实现不起作用

发布于 2025-02-07 20:41:26 字数 934 浏览 2 评论 0原文

我有4个看起来像这样的类:

public interface Foo<T> {
...
default boolean isEmpty() {
    return false; //dummy value real implementation is not relevant for the problem
}
}

public interface CharSequence { //java.lang.CharSequence
...
default boolean isEmpty() {
    return true; //dummy value real implementation is not relevant for the problem
}

public abstract class Bar<T> implements Foo<T> {
...
}

public final BarImpl extends Bar<Character> implements CharSequence { //typical diamond problem
...
@Override
public boolean isEmpty() { //needed to resolve diamond problem
   return Foo.super.isEmpty() // Compile Error: No enclosing instance of the type Foo<T> is accessible in scope
   return Bar.super.isEmpty() // Compile Error: No enclosing instance of the type Bar<T> is accessible in scope
   return CharSequence.super.isEmpty() // compiles
}

为什么我无法访问扩展栏的默认实现?

I have 4 Classes that look like this:

public interface Foo<T> {
...
default boolean isEmpty() {
    return false; //dummy value real implementation is not relevant for the problem
}
}

public interface CharSequence { //java.lang.CharSequence
...
default boolean isEmpty() {
    return true; //dummy value real implementation is not relevant for the problem
}

public abstract class Bar<T> implements Foo<T> {
...
}

public final BarImpl extends Bar<Character> implements CharSequence { //typical diamond problem
...
@Override
public boolean isEmpty() { //needed to resolve diamond problem
   return Foo.super.isEmpty() // Compile Error: No enclosing instance of the type Foo<T> is accessible in scope
   return Bar.super.isEmpty() // Compile Error: No enclosing instance of the type Bar<T> is accessible in scope
   return CharSequence.super.isEmpty() // compiles
}

Why can't I access the default implementation coming from extending Bar?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一瞬间的火花 2025-02-14 20:41:27

barimpl无法调用foo's 默认明确地方法,因为barimpl不是直接实现foo < /代码>。它正在扩展bar直接实现foo,因此,它是bar替代foo's默认方法方法与否。

barimpl只能调用bar's isempty()通过super.isempty(),可能最终会出现foo's 默认方法如果bar决定不覆盖它或以bar的具体方法(如果为

请注意,t.super.method()才能使用,t是直接实现的超级接口(即尚未由超级类或其他超级界面实现)或如果t是内部类的封闭类型。第二个用例是“ no enclosing实例…”的原因,在范围中可以访问”错误消息。

import java.util.Objects;

class Test {
  public static void main(String... arg) {
    System.out.println(new BarImpl().isEmpty());
  }
}

public interface Foo<T> {
  default boolean isEmpty() {
      System.out.println("  Foo's default method");
      return false;
  }
}

public abstract class Bar<T> implements Foo<T> {
}

public final class BarImpl extends Bar<Character> implements CharSequence {
  @Override
  public boolean isEmpty() {
    System.out.println("calling (Bar) super.isEmpty();");
    super.isEmpty();
    System.out.println("calling CharSequence.super.isEmpty();");
    return CharSequence.super.isEmpty();
  }

  public char charAt(int index) {
    Objects.checkIndex(index, length());
    return (char)('A' + index);
  }

  public int length() {
    System.out.println("  length() [CharSequence's default method]");
    return 26;
  }

  public CharSequence subSequence(int start, int end) {
    Objects.checkFromToIndex(start, end, length());
    return new StringBuilder(end - start).append(this, start, end);
  }

  public String toString() {
    return new StringBuilder(length()).append(this, 0, length()).toString();
  }
}
calling (Bar) super.isEmpty();
  Foo's default method
calling CharSequence.super.isEmpty();
  length() [CharSequence's default method]
false

BarImpl can not invoke Foo’s default method explicitly, as BarImpl is not directly implementing Foo. It’s extending Bar which directly implements Foo, hence, it’s Bar’s decision to override Foo’s default method or not.

BarImpl can only invoke Bar’s isEmpty() method via super.isEmpty(), which may end up at Foo’s default method if Bar decides not to override it or at a concrete method of Bar if it does override it.

Note that T.super.method() can only be used if either, T is a directly implemented super interface (i.e. not already implemented by a super class or another super interface) or if T is an enclosing type of an inner class. The second use case is the reason for the “No enclosing instance of … is accessible in scope” error message.

import java.util.Objects;

class Test {
  public static void main(String... arg) {
    System.out.println(new BarImpl().isEmpty());
  }
}

public interface Foo<T> {
  default boolean isEmpty() {
      System.out.println("  Foo's default method");
      return false;
  }
}

public abstract class Bar<T> implements Foo<T> {
}

public final class BarImpl extends Bar<Character> implements CharSequence {
  @Override
  public boolean isEmpty() {
    System.out.println("calling (Bar) super.isEmpty();");
    super.isEmpty();
    System.out.println("calling CharSequence.super.isEmpty();");
    return CharSequence.super.isEmpty();
  }

  public char charAt(int index) {
    Objects.checkIndex(index, length());
    return (char)('A' + index);
  }

  public int length() {
    System.out.println("  length() [CharSequence's default method]");
    return 26;
  }

  public CharSequence subSequence(int start, int end) {
    Objects.checkFromToIndex(start, end, length());
    return new StringBuilder(end - start).append(this, start, end);
  }

  public String toString() {
    return new StringBuilder(length()).append(this, 0, length()).toString();
  }
}
calling (Bar) super.isEmpty();
  Foo's default method
calling CharSequence.super.isEmpty();
  length() [CharSequence's default method]
false
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文