Scala access control modifier __scala

Source: Internet
Author: User
Tags modifier modifiers

Reference from "Programming in Scala" first, access control modifier

Package members, classes, or objects can be decorated with access control modifiers, such as private and protected, which control the access of other parts to these classes, objects. Scala and access control are generally similar to Java, but there are some important differences, which are described in this article. Private Members

Scala's private members are similar to Java, a class or object member decorated with private, accessible only in that class or object, and in Scala, in nested classes or objects. Like what:

Class Outer
{
    class Inner
    {
        private def f () {println ("F")}

        class innermost
        {
            f ()//ok
        }
    }

    (new Inner). f ()//error, F inaccessible
}

In Scala, (new Inner). F () is illegal because it is a private type defined in Inner, and access to f in innermost is legal because innermost is contained in the definition of Inner (child nested type).
In the Java language, both types of access are possible. Java allows external types to access private members of nested types that they contain. Protect Members

Like private members, Scala's access control is slightly stricter than Java's. In Scala, a member defined by protected can only be accessed by defining that member and its derived type. In Java, a member defined by protected can be accessed by other types in the same package. In Scala, this functionality can be done in other ways.

Here is an example of protected:

Package P
{
    class Super
    {
        protected def f () {println ("F")}
    }

    class Sub extends Super
    {
        f ()//ok
    }

    Class other
    {
        (new Super). F ()//error:f inaccessible
    }
}
Public Members

Public access control is the default method defined by Scala, and all members that are not decorated with private and protected (defined classes and methods) are "exposed", and such members can be accessed anywhere. Scala does not need to use public to specify the "open access" modifier.

Note: The classes and methods defined in Scala are public by default, but properties declared in the class are private by default. adding scopes for access control modifiers

Scala's access modifiers can add scope parameters. The syntax for the scope is as follows:
PRIVATE[X] or protected[x]
where x represents a package, a class, or a single instance object that can access this private or protected range until x.

By adding scope parameters to the access modifier, you can control very precisely the scope of the defined type that can be accessed by other types. In particular, package private,package protected, which supports Java language support, can be especially effective.

The following example is an example of this usage:

package bobsrockets {Package Navigation {//If private class Navigator, then class Navigat
        Or will only be visible for all types in the current package navigation.
        That is, private omits the [x],x as the current package or the current class or the current single Instance object by default.
        Private[bobsrockets] means that the class navigator is extended from the current package to all types in the Bobsrockets package. Private[bobsrockets] class Navigator {protected[navigation] def usestarchart () {} class L Egofjourney {Private[navigator] val distance = MB Private[this] V
            Ar speed =}} Package launch {Import Navigation._ object Vehicle {
            Private Val Guide: Indicates that guide is visible by default by the current single Instance object.
            Private[launch] Val Guide: Indicates that guide is visible by default to the current single Instance object to all types in the launch package. Private[launch] Val guide = New Navigator}}} 

In this example, the Class navigator is decorated with private[bobsrockets], which means that the class can be accessed by all types in the Bobsrockets package, for example, vehicle cannot access the private type Navigator in general, However, navigator can be accessed in vechile after using the package scope.

Private qualifiers can also be executed by the owning class and object. For example, class Legofjourney in code
The distance variable in is marked as Private[navigator], indicating that it is visible anywhere in the Navigator class. This access capability is consistent with the private members of the internal classes in Java.

All qualifiers can also be used for protected, which is the same as private meaning. That is, the protected[x modifier in Class C allows all subclasses and modifiers of C to have the package, class, or object X to access the definition with this tag. For example, the Usestartchart method in code can be accessed by all subclasses of the Class navigator and all code contained in navigator.

This technique is useful when dispersed across multiple Package large projects, allowing you to define some of the items that can be accessed in multiple packages but that are hidden from outside client code that uses these APIs, which is not possible in Java.

In addition, Scala supports a more restrictive access control than private, in this case, Private[this], which only allows access in the type that defines the member, which means that the member is not only accessible in the type that defines the member, but only by the type itself. This definition is called object-private. For example, in this case speed, using the protected[this] modifier, it is legal to access speed and this.speed within the Navigator class. However, a visit, even if it occurs within the Navigator class, is not allowed.

var other = new Navigator
other.speed//error: This row cannot be compiled

Mark a member as Private[this] to ensure that it cannot be accessed by other objects in the same class.

Note: If speed is decorated with private, the above code can be compiled.

Now, if you apply these combinations to legofjourney.distance, what will be the effect:

modifiers Public Access
Private[bobsrockets] accessing in the external package
Private[navigation] Same as Java package visibility (visible in package)
Private[navigator] Same as Java private (visible within a class)
Private[legofjourney] Same as private in Scala (visible in this class, not seen in the parent class)
Private[this] Visible only in the same object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.