假设我有两节课:

private[example] class PoorInt    extends Int
class RichInt    private[example] extends Int

问题是这些类声明中的private修饰符位置有什么区别?

最佳答案

第一个引用该类的范围,即,您不能在PoorInt包之外访问example

第二个是RichInt构造函数的范围,您没有明确提供它,即您不能在example包之外使用它的构造函数。

例如:

// somewhere outside package example ...
val x = new RichInt // doesn't compile. Constructor is private
val y : PoorInt = ... // doesn't compile. PoorInt is not accessible from here
def f(x: PoorInt) = ... // doesn't compile. PoorInt is not accessible from here

您还可以看到this question

10-08 17:17