private[this]
2014-06-09 00:00
162 查看
摘要: private private[this]
In Scala (as well as in Java or C++), a method can access the private fields of all objects of its class.
For example,
Accessing other.value is legal because other is also a Counter object.
Scala allows an even more severe access restriction, with the private[this] qualifier:
Now, the methods of the Counter class can only access the value field of the current object, not of other objects of type Counter. This access is sometimes called object-private, and it is common in some OO languages, such as SmallTalk.
With a class-private field, Scala generates private getter and setter methods. However, for an object private field,no getters and setters are generated at all.
Note:
Scala allows you to grant access rights to specific classes. The private[ClassName] qualifier states that only methods of the given class can access the given field. Here, the ClassName must be the name of the class being defined or an enclosing class. (See Section 5.8, “Nested Classes,” on page 60 for a discussion of inner classes.) In this case, the implementation will generate auxiliary getter and setter methods that allow the enclosing class to access the field. These methods will be public because the JVM does not have a fine-grained access control system, and they will have implementation-dependent names.
In Scala (as well as in Java or C++), a method can access the private fields of all objects of its class.
For example,
class Counter { private var value = 0 def increment() { value += 1 } def isLess(other: Counter) = value < other.value // Can access private field of other object }
Accessing other.value is legal because other is also a Counter object.
Scala allows an even more severe access restriction, with the private[this] qualifier:
private[this] var value = 0 // Accessing someObject.value is not allowed
Now, the methods of the Counter class can only access the value field of the current object, not of other objects of type Counter. This access is sometimes called object-private, and it is common in some OO languages, such as SmallTalk.
With a class-private field, Scala generates private getter and setter methods. However, for an object private field,no getters and setters are generated at all.
Note:
Scala allows you to grant access rights to specific classes. The private[ClassName] qualifier states that only methods of the given class can access the given field. Here, the ClassName must be the name of the class being defined or an enclosing class. (See Section 5.8, “Nested Classes,” on page 60 for a discussion of inner classes.) In this case, the implementation will generate auxiliary getter and setter methods that allow the enclosing class to access the field. These methods will be public because the JVM does not have a fine-grained access control system, and they will have implementation-dependent names.
相关文章推荐
- Java基础之面向对象(一)--特性、类和对象关系、this、private
- JAVA基础再回首(四)——面向对象思想、类与对象、成员/局部变量、匿名对象、封装、private、this、构造方法
- MSVCRT.lib(ti_inst.obj) : error LNK2005: "private: __thiscall type_info::type_info(class type_info c
- JAVA笔记之private、构造函数、this
- The JAR of this class file belongs to container 'Android Private Libraries' Android查看外部依赖jar的源码问题解
- Java基础教程24-public/protected/private/this介绍
- scala private private[this]的区别
- Java基础之面向对象(一)--特性、类和对象关系、this、private
- 4.一些关简单的键字.package,private,this
- VC编译错误:msvcrtd.lib(ti_inst.obj) : error LNK2005: "private: __thiscall type_info::type_info(class typ
- Xampp – Open SSL – Don’t know how to get public key from this private key
- static,this,private关键字用法
- error LNK2005: "private: __thiscall type_info::type_info(class&n
- Scala访问权限修饰符:private和private[this]
- Trouble with the private key for “myName” is not installed on this mac
- static,this,private关键字用法
- scala基础9-类及对象私有属性private[this]
- Trouble with the private key for “myName” is not installed on this mac
- 类与对象,类的构造方法和初始化块,private修饰符,this,继承与父类
- Java基础-07.总结private,this,封装,static,成员方法变量,局部变量匿名对象