它说,在Java教程"定义接口"中
如果未指定接口
public,则只能在与接口相同的包中定义的类上访问您的接口.
但是,这个
interface PPInterface {
void foo();
void bar();
}
class NewClass implements PPInterface {
void foo() {}
void bar() {}
}
Run Code Online (Sandbox Code Playgroud)
生成编译器错误NewClass因为我'试图分配较弱的访问权限; 是公开的.所以文档是错误的,或者我做错了什么,或者我误解了文档?
我想我不必使用界面 - 我喜欢它,因为它保持了良好的组织.
public class TestImpl {
public static void main(String[] args) {
HelloImpl h1 = new HelloImpl(), h2 = h1;
h1.message = "holla";
System.out.println(h2.sayHello());
}
}
interface Hello {
String sayHello();
}
class HelloImpl implements Hello {
static String message = "Hello";
String sayHello() {
return message;
}
}
Run Code Online (Sandbox Code Playgroud)
我得"试图分配较弱的特权".