gpt4 book ai didi

java - 为什么接口(interface)不能有 protected 方法

转载 作者:IT老高 更新时间:2023-10-28 20:35:24 28 4
gpt4 key购买 nike

Possible Duplicate:
Protected in Interfaces

在 Java 中,为什么我不能在接口(interface)中使用 protected 方法?

因为根据 Java 规范

protected access (denoted by the keyword protected) - a field or method accessible to any type in the same package, and to subclasses in any package.

如果我必须使用接口(interface),我将实现它并覆盖方法。因此,如果我要实现类可以访问这些方法的位置,因为可以在任何包中访问方法。那么在接口(interface)中声明方法 protected 有什么危害呢?

最佳答案

protected 方法旨在与子类共享实现。就实现共享而言,接口(interface)没有什么可提供的,因为它们根本没有实现。因此接口(interface)上的所有方法都必须是公共(public)的。

关于java - 为什么接口(interface)不能有 protected 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9046012/

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