代码之家  ›  专栏  ›  技术社区  ›  Hatefiend

作为功能接口的通用函数?

  •  0
  • Hatefiend  · 技术社区  · 6 年前

    我有以下函数,它返回 List 与传递它的类型相同。

    <T> List<T> foo(T bar)
    {
        ...
    }
    

    我想有同样的功能,除了在形式 Java Functional Interface . 我试过以下方法:

    final <T> Function<T, List<T>> foo;
    

    但它不喜欢 <T> . 如果我忽略了 <T & GT; 像:

    final Function<T, List<T>> foo;
    

    它声称找不到类型 T ,错误与我将原始函数定义为:

    List<T> foo(T bar) // cannot find type 'T'
    {
        ...
    }
    

    我想发挥作用 foo 作为一级函数。

    1 回复  |  直到 6 年前
        1
  •  3
  •   Bohemian    6 年前

    T

    class MyClass<T> implements Function<T, List<T>> {
        public List<T> accept(T t) {
             // some impl
        }
    }
    
    推荐文章