Kotlin中受包装保护的替代产品 [英] package protected alternative in kotlin

查看:98
本文介绍了Kotlin中受包装保护的替代产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,我们为类提供了受包保护(默认)的修饰符,该修饰符允许在一个包中包含多个类,但只公开少数几个并保持逻辑封装.

In Java we have package protected (default) modifier for classes, which allows to have many classes in a single package but expose only few and keep the logic encapsulated.

对于Kotlin来说,情况似乎并非如此,如果我想让其他几个类对彼此可见,那么我就不必再使用private修饰符了,它只能将可见性限制在一个文件中.因此,从本质上讲,如果您在一个程序包中有10个类,而现在只有其中一个是公开的,则您将拥有一个包含所有类的巨大文件(并且到处都是private)...

With Kotlin this seems not to be the case, if I want to have few other classes that shall be visible to each other, but not further I have to use private modifier which limits visibility to a single file. So essentially if you had 10 classes in a package and only one of those were public now you'll have one huge file with all the classes in it (and private all over the place)...

这是正常的做法还是有办法在Kotlin中实现一些类似的模块化?

Is this normal practice or there is a way to achieve the some similar modularity in Kotlin?

我不明白:如果他们有软件包的概念,为什么他们要摆脱软件包保护的访问方式...

I don't understand: if they have notion of package, why did they get rid of package protected access...

更新:毕竟我们可能具有受软件包保护的可见性
在此处查看讨论

更新:如果您仔细阅读了讨论内容,但仍然认为这是该语言的必备功能,请在此处投票. 推荐答案

与Java相比,Kotlin似乎对包模型的依赖程度较低(例如,目录结构未绑定到包).相反,Kotlin提供了 internal可见性,模块化项目架构.使用它,您可以将部分代码封装在单独的模块

Kotlin, compared to Java, seems to rely on packages model to a lesser degree (e.g. directories structure is not bound to packages). Instead, Kotlin offers internal visibility, which is designed for modular project architecture. Using it, you can encapsulate a part of your code inside a separate module.

因此,您可以在顶层声明中使用

So, on top level declarations you can use

  • private限制文件的可见性
  • internal以限制对模块的可见性
  • private to restrict visibility to the file
  • internal to restrict visibility to the module

目前,没有其他限制可见性的选项.

At this point, there is no other option for visibility restriction.

这篇关于Kotlin中受包装保护的替代产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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