【发布时间】:2020-06-23 16:39:11
【问题描述】:
我正在尝试将扩展集合实现为接口以及不断向我发送"Non-static method 'of(java.util.Collection<ELEMENT_TYPE>)'" cannot be referenced from a static context" 的方法
我知道它已在其他线程上得到回答,但所有这些答案都不能解决我的问题。
提前致谢。
编译器告诉我
**
Error:(22, 54) java: non-static method <ELEMENT_TYPE>of(java.util.Collection<ELEMENT_TYPE>) cannot be referenced from a static context
Error:(31, 62) java: non-static method <ELEMENT_TYPE>of(java.util.Collection<ELEMENT_TYPE>) cannot be referenced from a static context
Error:(22, 63) java: cannot find symbol
symbol: method map(this::mapper)
location: interface java.util.Collection
Error:(31, 71) java: cannot find symbol
symbol: method toMap((it)->it.s[...]0, 2))
location: interface java.util.Collection
Error:(40, 64) java: cannot find symbol
symbol: method toSet()
location: interface java.util.Collection
Error:(40, 55) java: non-static method <ELEMENT_TYPE>of(java.util.Collection<ELEMENT_TYPE>) cannot be referenced from a static context
**
我确定我无权编辑测试文件
编辑:
我将 switch 重写为:
static <ELEMENT_TYPE> ExtendedCollection<ELEMENT_TYPE> of(Collection<ELEMENT_TYPE> list)
{
ExtendedCollection<ELEMENT_TYPE> c = new Collection<ELEMENT_TYPE>();
list.forEach(e -> c.add(e));
return (ExtendedCollection<ELEMENT_TYPE>) c;
}
它现在几乎可以工作了我不知道如何实例化扩展集合,因为它是一个接口
【问题讨论】:
-
请标记收到错误消息的确切行。
-
我添加了他们感谢您的快速回答
-
我们仍然不知道您的源文件中的第 22 行或第 31 行是什么。请在上面列出的源文件中标记出现错误的行。就像实际上放一个注释表明这是有错误的行。
-
您应该在问题中包含
ExtendedCollection的定义。此外,请在问题中指定确切的要求,而不是期望读者向下滚动并从附加到不同答案的 cmets 中收集位和字节。
标签: java generics collections interface extends