“无法降低继承方法的可见性”的含义有一个界面 [英] Meaning of "Cannot reduce the visibility of the inherited method" with an interface

查看:465
本文介绍了“无法降低继承方法的可见性”的含义有一个界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件:

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){表明:

I thought the code looks okay, but I am getting an error in the second file for the line void print(PrintDetails details) { that states:


不能从 PrintService 降低继承方法的可见性。

Cannot reduce the visibility of the inherited method from PrintService.

有人可以解释一下这对我意味着什么?

Can someone explain what this means for me?

推荐答案

在Java界面中,默认情况下每个方法都是 public

In a Java interface each method is by default 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. [..]

在实施类中,不允许降低可见性,也不允许指定访问修饰符:

In an implementing class you are not allowed to reduce the visibility, and by not specifying an access modifier:

void print(){..}

您正在指定访问级别 默认 ,其可见度低于 public

you are specifying the access level default, which has lower visibility than public.

这篇关于“无法降低继承方法的可见性”的含义有一个界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆