我有以下代码:
class Hello {
class Thing {
public int size;
Thing() {
size = 0;
}
}
public static void main(String[] args) {
Thing thing1 = new Thing();
System.out.println("Hello, World!");
}
}
Run Code Online (Sandbox Code Playgroud)
我知道Thing什么都不做,但我的Hello,World程序在没有它的情况下编译得很好.这只是我定义的类失败了.
它拒绝编译.我开始No enclosing instance of type Hello is accessible."创造一个新的东西.我猜是:
有任何想法吗?
我正在尝试编写一个集合接口库,它使用Java 8中的新默认方法语法实现标准Collection API中的大多数方法.以下是我想要的一小部分示例:
public interface MyCollection<E> extends Collection<E> {
@Override default boolean isEmpty() {
return !iterator().hasNext();
}
//provide more default overrides below...
}
public interface MyList<E> extends MyCollection<E>, List<E> {
@Override default Iterator<E>iterator(){
return listIterator();
}
//provide more list-specific default overrides below...
}
Run Code Online (Sandbox Code Playgroud)
但是,即使这个简单的例子也遇到了编译器错误:
error: interface MyList<E> inherits abstract and default
for isEmpty() from types MyCollection and List
Run Code Online (Sandbox Code Playgroud)
根据我对默认方法的理解,应该允许这样做,因为只有一个扩展接口提供了默认实现,但显然情况并非如此.这里发生了什么?有没有办法让这个做我想要的?