The Scala access modifier is basically the same as Java’s: private,protected,public.
If no access modifier is specified, the access level of the Scala object is public by default.
In Scala Use But in These two kinds of access are allowed in Java because it allows external classes to access private members of inner classes. In scala, access to Protected members is more stringent than java. Because it only allows protected members to be accessed in subclasses of the class in which the member is defined. In java, use the In the above example, there is no problem with Sub class access to f because f is declared as In Scala, if no modifier is specified, it defaults to public. Such members can be accessed anywhere. In Scala, access modifiers can be emphasized by using qualifiers. The formatis: The x here refers to a package, class, or singleton object to which it belongs. If written as This technique is useful in large projects that span several packages, allowing you to define things that are visible in several subpackages of your project but are always invisible to customers outside the project. In the above example, the class Navigator is marked as For example, from
private
qualifiers, which are stricter than Java, in the case of nested classes, the outer class cannot even access the private members ofthe nested class. 8.7.1. Private member #
private
keyword modifier, the member with this tag is visible only within the class or object that contains the member definition, and the samerule applies to the inner class.Example #
class Outer{
class Inner{
private def f(){
println("f")
}
class InnerMost{
f() // correct
}
}
(new Inner).f() //error
}
(new
Inner).f(
)
the access is illegal because f is in
Inner
, declared as
private
and access is not in the class
Inner
within.
InnerMost
there is no problem accessing f in, because this access is included in the
Inner
within the class. 8.7.2. Protected members #
protected
keyword modified members can be accessed not only by subclasses of the class that defines the member, but also by other classes in the same package.Example #
package p {
class Super {
protected def f() {println("f")}
}
class Sub extends Super {
f()
}
class Other {
(new Super).f() //error
}
}
protected
and Sub is a subclass of Super In contrast, other’s access to f is not allowed because other does not inherit from Super. The latter is also recognized in java because Other and Sub are in the same package. 8.7.3. Public member #
Example #
class Outer {
class Inner {
def f() { println("f") }
class InnerMost {
f() // correct
}
}
(new Inner).f() // Correct because f() is public
}
8.7.4. Scope protection #
private[x]
or
protected[x]
private[x]
read as “this member in addition to the
[…]
, the class in or
[…]
classes in the package and their concomitant objects are visible to all other classes
private
.Example #
package bobsrockets{
package navigation{
private[bobsrockets] class Navigator{
protected[navigation] def useStarChart(){}
class LegOfJourney{
private[Navigator] val distance = 100
}
private[this] var speed = 200
}
}
package launch{
import navigation.\_
object Vehicle{
private[launch] val guide = new Navigator
}
}
}
private[bobsrockets]
, that is, this class pair is contained in the
bobsrockets
all classes and objects in the package are visible.
Vehicle
right in the object.
Navigator
access is allowed because the object
Vehicle
included in the package
launch
, and
launch
wrapped in
bobsrockets
on the contrary, allin the package
bobsrockets
none of the outside code can access the class
Navigator
.