【问题标题】:use method from generic interface使用泛型接口中的方法
【发布时间】:2021-04-17 22:10:53
【问题描述】:

我有一个这样的界面:

public interface Segments <T> {
 List<T> getSegments(); 
}

还有一个班级:

public class Tree<S, T extends Segmentable<S>>{
}

我想实现和使用接口中的 getSegments() 方法,但不知道如何。

【问题讨论】:

    标签: java class generics interface


    【解决方案1】:

    您需要在类中实现接口,如下所示:

    public class Tree<S, T extends Segmentable<S>> implements Segments<S> {
        
        @Override
        public List<S> getSegments() {
            // Return some List<S> that you want here.
        }
    }
    

    或者您需要在已经实现它的对象上调用getSegments() 方法。

    【讨论】:

      猜你喜欢
      • 2017-11-13
      • 2014-12-15
      • 1970-01-01
      • 2011-03-14
      • 1970-01-01
      • 1970-01-01
      • 2012-03-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多