博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Scala访问修饰符
阅读量:7223 次
发布时间:2019-06-29

本文共 2216 字,大约阅读时间需要 7 分钟。

hot3.png

Scala访问修饰符

基本和Java的访问修饰符一样。在scala 中也有访问修饰符,如下

Members of packages, classes, or objects can be labeled with the access modifiers private and protected, and if we are not using either of these two keywords, then access will be assumed as public. These modifiers restrict(限制和限定) accesses to the members to certain regions of code. To use an access modifier, you include its keyword in the definition of members of package, class or object as we will see in the following section.

Private members

A private member is visible only inside the class or object that contains the member definition. Following is the example:

class Outer {   class Inner {      private def f() { println("f") }      class InnerMost {         f() // OK      }   }   (new Inner).f() // Error: f is not accessible}

In Scala, the access (new Inner).f() is illegal because f is declared private in Inner and the access is not from within class Inner. By contrast, the first access to f in class InnerMost is OK, because that access is contained in the body of class Inner. Java would permit both accesses because it lets an outer class access private members of its inner classes.

Protected members

A protected member is only accessible from subclasses of the class in which the member is defined. Following is the example:

package p {   class Super {      protected def f() { println("f") }   }   class Sub extends Super {      f()   }   class Other {     (new Super).f() // Error: f is not accessible   }}

The access to f in class Sub is OK because f is declared protected in Super and Sub is a subclass of Super. By contrast the access to f in Other is not permitted, because Other does not inherit from Super. In Java, the latter access would be still permitted because Other is in the same package as Sub.

Public members

Every member not labeled private or protected is public. There is no explicit modifier for public members. Such members can be accessed from anywhere. Following is the example:

class Outer {   class Inner {      def f() { println("f") }      class InnerMost {         f() // OK      }   }   (new Inner).f() // OK because now f() is public}

=============END=============

转载于:https://my.oschina.net/xinxingegeya/blog/489562

你可能感兴趣的文章
perf之record
查看>>
C#中的数据格式转换 (未完待更新)
查看>>
启动vsftpd失败
查看>>
yii2组件之下拉框带搜索功能(yii-select2)
查看>>
Java串口通信详解
查看>>
Newtonsoft 自定义输出内容
查看>>
HTML图片元素(标记)
查看>>
windows server 2008 域控安装
查看>>
编写高质量代码:改善Java程序的151个建议(第1章:JAVA开发中通用的方法和准则___建议6~10)...
查看>>
Oracle查看和修改连接数(进程/会话/并发等等)
查看>>
【SpringMVC学习06】SpringMVC中的数据校验
查看>>
Laravel错误与日志处理
查看>>
微信小程序开发教程第七章:微信小程序编辑名片页面开发
查看>>
Java并发编程:Java ConcurrentModificationException异常原因和解决方法
查看>>
浅谈iOS中MVVM的架构设计
查看>>
node.js 中模块的循环调用问题详解
查看>>
ActiveReports 报表应用教程 (6)---分组报表
查看>>
OLEDB操作Excel
查看>>
struts2的json-default和struts-default的区别
查看>>
java中<> 的用法
查看>>