首页 > 解决方案 > 在 Eclipse 中使用泛型密封接口

问题描述

以下是合法的(即我可以编译它)并在启用预览功能的 Java 15 中工作(在 eclipse 2020-09 中)

public sealed interface Quantity<T> permits QuantityImpl { }
public record QuantityImpl<T extends Number>(T value) implements Quantity<T> { }

public class Play {
    public static void main(String[] args) {
        Quantity<Float> q = new QuantityImpl<>(3.0f);
        System.out.println(q instanceof Quantity);
    }
}

Quantity.java但是,日食在抱怨... permits QuantityImpl。当悬停在QuantityImpl一个可以阅读:

Permitted type QuantityImpl does not declare Quantity<T> as direct super interface

即使它编译,这是一个有效的投诉还是 eclipse 中的错误?

标签: javaeclipseecjjava-15java-sealed-type

解决方案


推荐阅读