Ala*_*an2 7 java interface access-specifier
我有两个文件:
public interface PrintService {
void print(PrintDetails details);
class PrintDetails {
private String printTemplate;
}
public interface Task {
String ACTION = "print";
}
}
Run Code Online (Sandbox Code Playgroud)
和
public class A implements PrintService {
void print(PrintDetails details) {
System.out.println("printing: " + details);
}
String action = PrintService.Task.ACTION;
}
Run Code Online (Sandbox Code Playgroud)
我认为代码看起来没问题,但是我在第二个文件中收到错误信息void print(PrintDetails details) {:
无法降低继承方法的可见性
PrintService.
有人能解释这对我意味着什么吗?
Jig*_*shi 23
在Java接口中,默认情况下每个方法都是public:
接口主体中的每个方法声明都是隐式抽象的,因此它的主体始终用分号表示,而不是块.
接口主体中的每个方法声明都是隐式公开的.[..]
在实现类中,不允许降低可见性,也不允许指定访问修饰符:
void print(){..}
Run Code Online (Sandbox Code Playgroud)
您正在指定访问级别默认值,其可见性低于public.