Get the App
SLTechnology News&Howtos  ›  Servers  › 

Learn a little bit of Scala's class member property modifiers every day

Shulou Source: shulou.com Published: 2022-06-02 15:22:08 09月20日 Update

In scala, the modifiers for the member properties of a class may have the following situations:

Case 1: use var to decorate

In the class, you can get and modify

Class, which can be obtained or modified

Case 2: use val to modify

In a class, you can only get it, not modify it.

In an object, it can only be obtained, not modified.

Case 3: use private var to decorate

In the class, you can get and modify

But,

Object, cannot be obtained, modified

Case 4: use privateval to decorate

In a class, you can only get it, not modify it.

But,

Object, cannot be obtained, modified

Case 5: decorate with private [this] var

In the class, you can get and modify

This kind of member property, which belongs to the object level, can only be used in the current instance; it cannot be used by passing other objects.

Case 6: use private [this] val modification

In a class, you can only get it, not modify it.

This kind of member property, which belongs to the object level, can only be used in the current instance; it cannot be used by passing other objects.

In short:

A, see the var modifier, description, class and object, this property can be obtained and modified

B. See the val modifier, indicating that in classes and objects, this property can only be obtained, not modified, but cannot be reassigned.

C. See the private var, privateval modifier, which is private at the class level and private to the class; it can only be used inside the class, but not the object; in other words, the access to the member attribute is limited to the inside of the class, and the object cannot be accessed, let alone modified.

D, see modifiers similar to private [this], indicating that member attributes are more strictly restricted and can only be used in the current instance object and cannot be passed through other objects.

Tags: Objects properties conditions members instances levels to make private restrictions that is permissions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Tech Info Apple vpn MySQL