Latest web development tutorials

Scala access modifier

Scala and Java access modifiers, like basic, respectively: private, protected, public.

If you do not specify an access modifier character, by default, the access level Scala objects are public.

Scala qualifier in private, more stringent than Java, in the case of nested class, the outer class can not even be nested class access private members.


Private (Private) members

Modified with private keyword, members with this mark only contains the class or object member defined internal visible, the same rule also applies to inner classes.

class Outer{
    class Inner{
    private def f(){println("f")}
    class InnerMost{
        f() // 正确
        }
    }
    (new Inner).f() //错误
}

(New Inner) .f () access is not legal in Inner because f is declared as private, but access is not within the category of Inner.

Access f but there is no problem in InnerMost, because this visit is included in the class Inner.

Java is allowed access to both, because it allows the outer class access private members of inner classes.


Protection (Protected) members

In the scala, the access protection (Protected) members are more stringent than the java. Because it only allows the protection of members of the subclass is defined in the member of the class is accessed. In java, use the keyword protected modified members, in addition to the definition of a subclass of the class can access the member, with a package of other classes can also be accessed.

package p{
class Super{
    protected def f() {println("f")}
    }
	class Sub extends Super{
	    f()
	}
	class Other{
		(new Super).f() //错误
	}
}

The above example, Sub type of access is not a problem for f, because f is declared as protected in the Super, and Super Sub is a subclass. In contrast, Other is not allowed access to the f, because there is no other inherited from the Super. While the latter also being recognized in java, because with Other Sub in the same bag.


Public (Public) members

Scala, if you do not specify any modifiers, the default is public. Such a member can be accessed anywhere.

class Outer {
   class Inner {
      def f() { println("f") }
      class InnerMost {
         f() // 正确
      }
   }
   (new Inner).f() // 正确因为 f() 是 public
}

The scope of protection

Scala, the access modifier can be emphasized by using qualifiers. The format is:

private[x] 

或 

protected[x]

Where x refers to a package belongs to a class or singleton object. If written in private [x], read "In addition to the members of the [...] or in the class [...] of the classes in packages and their associated image visible on the outside, for all other classes are private.

This technique is useful across a number of large projects in the package, which allows you to define several sub-package in your project visible external customers but the project has always invisible things.

package bobsrocckets{
    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
        }
    }
}

The above example, the class Navigator is marked as private [bobsrockets] That is the class of all classes and objects contained in the package bobsrockets visible.

For example, from the Vehicle access to the object in the Navigator is permitted because the object Vehicle included in the package launch, whereas in bobsrockets launch package, on the contrary, all of the code outside the package bobsrockets can access class Navigator.