gpt4 book ai didi

java - 带有接口(interface)的 "Cannot reduce the visibility of the inherited method"的含义

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:00:27 24 4
gpt4 key购买 nike

我有两个文件:

public interface PrintService {
void print(PrintDetails details);
class PrintDetails {
private String printTemplate;
}
public interface Task {
String ACTION = "print";
}
}

public class A implements PrintService {
void print(PrintDetails details) {
System.out.println("printing: " + details);
}
String action = PrintService.Task.ACTION;
}

我认为代码看起来没问题,但我在第二个文件中收到错误,行 void print(PrintDetails details) { 指出:

Cannot reduce the visibility of the inherited method from PrintService.

谁能解释一下这对我意味着什么?

最佳答案

在 Java 接口(interface)中,每个方法默认都是public:

Every method declaration in the body of an interface is implicitly abstract, so its body is always represented by a semicolon, not a block.

Every method declaration in the body of an interface is implicitly public. [..]

在实现类中,不允许通过不指定访问修饰符来降低可见性:

void print(){..}

您正在指定访问级别 default ,其可见性低于 public

关于java - 带有接口(interface)的 "Cannot reduce the visibility of the inherited method"的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11031562/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com