Java 中的接口:不能使实现的方法受保护或私有 [英] Interfaces in Java: cannot make implemented methods protected or private

查看:25
本文介绍了Java 中的接口:不能使实现的方法受保护或私有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道接口必须是公开的.但是,我不想要那样.

I know that an interface must be public. However, I don't want that.

我希望我实现的方法只能从它们自己的包中访问,所以我希望我实现的方法受到保护.

I want my implemented methods to only be accessible from their own package, so I want my implemented methods to be protected.

问题是我无法保护接口或实现的方法.

The problem is I can't make the interface or the implemented methods protected.

什么是变通方法?是否有与此问题相关的设计模式?

What is a work around? Is there a design pattern that pertains to this problem?

根据 Java 指南,抽象类也无法完成这项工作.

From the Java guide, an abstract class wouldn't do the job either.

推荐答案

阅读 这个.

"公共访问说明符表示该接口可以被任何包中的任何类使用.如果您不指定该接口是公共的,则您的接口将只能被定义在相同包中的类访问界面."

"The public access specifier indicates that the interface can be used by any class in any package. If you do not specify that the interface is public, your interface will be accessible only to classes defined in the same package as the interface."

这就是你想要的吗?

这篇关于Java 中的接口:不能使实现的方法受保护或私有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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