Scala 中带有方括号 (private[...]) 的私有范围 [英] Private scoping with square brackets (private[...]) in Scala

查看:41
本文介绍了Scala 中带有方括号 (private[...]) 的私有范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在查看 Gatling 源代码时遇到了以下语法:

I've come across the following syntax while looking through the Gatling source code:

private[http] def build = {
  // ...
}

方括号内的语法是什么?

What is the syntax inside square brackets?

当我在 IDE 中单击它时,它是完全限定包 (com.excilys.ebi.gatling.http) 的别名,但我找不到该别名的定义位置.

When I click through it in my IDE it is an alias to a fully qualified package (com.excilys.ebi.gatling.http) but I can't find where that alias was defined.

推荐答案

参见 scala 参考,特别是第 5.2 章.部分摘录:

See the scala reference, specifically, chapter 5.2. Some excerpt:

私有修饰符可以与模板中的任何定义或声明一起使用.这些成员只能从直接封闭的模板及其伴随模块或伴随类(第 5.4 节)中访问.他们是不被子类继承,并且它们不能覆盖父类中的定义类.

The private modifier can be used with any definition or declaration in a template. Such members can be accessed only from within the directly enclosing template and its companion module or companion class (§5.4). They are not inherited by subclasses and they may not override definitions in parent classes.

修饰符可以用标识符 C(例如 private[C]) 限定,该标识符必须表示包含定义的类或包.标记为的成员这样的修饰符只能分别从包 C 中的代码或只能从类 C 及其伴随模块中的代码访问(第 5.4 节).此类成员也仅从 C 中的模板继承.

The modifier can be qualified with an identifier C (e.g. private[C]) that must denote a class or package enclosing the definition. Members labeled with such a modifier are accessible respectively only from code inside the package C or only from code inside the class C and its companion module (§5.4). Such members are also inherited only from templates inside C.

这篇关于Scala 中带有方括号 (private[...]) 的私有范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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