Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report