8.7. Scala access modifier

发布时间 : 2025-10-25 13:35:15 UTC      

Page Views: 9 views

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 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 #

Use 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.

But in InnerMost there is no problem accessing f in, because this access is included in the Inner within the class.

These two kinds of access are allowed in Java because it allows external classes to access private members of inner classes.

8.7.2. Protected members #

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 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 } } 

In the above example, there is no problem with Sub class access to f because f is declared as 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 #

In Scala, if no modifier is specified, it defaults to public. Such members can be accessed anywhere.

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 #

In Scala, access modifiers can be emphasized by using qualifiers. The formatis:

private[x] or protected[x] 

The x here refers to a package, class, or singleton object to which it belongs. If written as 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 .

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.

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 } } } 

In the above example, the class Navigator is marked as private[bobsrockets] , that is, this class pair is contained in the bobsrockets all classes and objects in the package are visible.

For example, from 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 .

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。