【问题标题】:Why are methods of extended interface not visible in class that implements extending interface?为什么扩展接口的方法在实现扩展接口的类中不可见?
【发布时间】:2017-05-04 08:21:01
【问题描述】:

我对 java.time API 有疑问,特别是 LocalDate。

LocalDate 实现 ChronoLocalDate,而 this 实现 Comparable。

当我做类似的事情时

LocalDate now = LocalDate.now();
now.getClass().getInterfaces();

它只列出了直接在 LocalDate 的接口,而不是 ChronoLocalDate 继承的接口。为什么会这样?

附加组件:我最初的问题是我有一个通用类处理实现可比较的类型,但这显然不适用于 LocalDate,因为它不直接实现 Comparable。

下面是泛型类的定义:

public final class Range<T extends Comparable<T> & Serializable> implements Serializable

我需要做什么才能使这个泛型类定义适用于 LocalDate 类型?

【问题讨论】:

    标签: java generics interface java-8


    【解决方案1】:

    你的定义是不是应该更像这样:

    static final class Range<T extends Comparable<? super T> &   
              Serializable> implements Serializable {
    

    【讨论】:

    • 很好的输入谢谢,我知道定义需要看起来不同,但我不清楚如何将此引用添加到已实现的接口。不过,您是否也知道我的第一个问题(对我来说只是一个哲学问题)的答案,为什么继承的接口方法没有被它检索到?
    • @hecko84 因为javadoc这么说? Returns the interfaces directly implemented by the class or interface represented by this object。有办法得到它们; guava 通过 TypeToken 提供此功能。
    • 好的,我明白了,有时阅读会有所帮助,我只是没有看。感谢有关番石榴的提示和通用定义。
    猜你喜欢
    • 2018-05-22
    • 1970-01-01
    • 2012-11-06
    • 2013-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-25
    • 2016-04-28
    相关资源
    最近更新 更多