修饰符“保护"不适用于Kotlin中的“文件"内部错误 [英] Modifier `protected` is not applicable inside `file` error in Kotlin

查看:245
本文介绍了修饰符“保护"不适用于Kotlin中的“文件"内部错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个声明如下的类:

I have a class whose declaration is like :

class NetworkManagerImpl : NetworkManager { }

我想使该类受保护,以便仅在包中可见.但是当我在类的前面添加保护时:

I wanted to make the class protected so that it is visible inside package only. But when i add protected in front of class like :

protected class NetworkManagerImpl : NetworkManager { }

它给出错误,因为修饰符protectedfile内不适用 如何解决此错误,或更重要的是如何使整个类(顶层)受到保护?

It gives error as Modifier protected is not applicable inside file How to fix this error or more importantly how to make an entire class(Top level) protected?

推荐答案

文档,对于类之类的顶级实体没有protected修饰符.

As written in the docs, there is no protected modifier for top-level entities like classes.

以下是声明来自Kotlin团队成员:

Here’s a statement coming from a Kotlin team member:

不具有程序包保护的访问的动机非常简单:它不提供任何实际的封装.系统中的任何其他模块都可以在与复杂的独立组件相同的程序包中定义类,并可以完全访问其内部.另一方面,具有内部可见性的类只能从定义了该类的模块中访问,而不能从任何其他模块访问.

The motivation for not having package protected access is very simple: it does not provide any real encapsulation. Any other module in the system can define classes in the same package as your complex independent component and get full access to its internals. On the other hand, classes with internal visibility cannot be accessed from any module other than the one where they are defined.

这篇关于修饰符“保护"不适用于Kotlin中的“文件"内部错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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