简体   繁体   中英

What is the difference between private modifier position in a Scala class declaration?

Suppose I have two classes:

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

The question is what's the difference between private modifier position in these classes declarations?

The first refers to the scope of the class, ie you cannot access PoorInt outside package example .

The second refers to the scope of the constructor of RichInt , which you are not providing explicitly, ie you cannot use its constructor outside package example .

For 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

You can also see this question .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM