In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to use the Bean attribute of Scala". In the daily operation, I believe that many people have doubts about how to use the Bean attribute of Scala. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt of "how to use the Bean attribute of Scala". Next, please follow the editor to study!
First, category
Class SimpleClass {
Var value = 0
}
Val c = new SimpleClass
A simple class is defined above, which doesn't look much different from that in java. Another problem involved here is that if a method has no parameters, the following () is optional.
II. Getter and setter
Or the above class, let's take a look at what the compiled class file looks like, the value property becomes private, and generates two methods, although not quite the same as java's get&set, but the nature is actually the same. The special thing is that value_$eq, this $eq represents =, and since = in java cannot appear in the method name, use $eq instead.
Public class SimpleClass
{
Private int value = 0
Public int value () {return this.value;}
Public void value_$eq (int Xero1) {this.value = Xero1;}
}
At this time, you can access this property normally.
Val c = new SimpleClass
C.value = 100
Val tmp = c.value
Only getter
Val onlyget = 100
Corresponding java code
Private final int onlyget = 100
Public int onlyget () {
Return this.onlyget
}
IV. Private object
There is private in scala, which has the same meaning as in java, except that there is private [this] in scala. Look at this code first, there is no problem with this kind of code.
Class SimpleClass {
Private var value = 0
Def < (that: SimpleClass) = this.value < that.value
}
If you change it to look like this, there will be a problem. That.value cannot be compiled and passed. Private [this] means that it is valid only for the current object, that is, this.
Class SimpleClass {
Private [this] var value = 0
Def < (that: SimpleClass) = this.value < that.value
}
5. Bean attribute
We can also build more java get and set, and note that import scala.beans.BeanProperty is a must
Import scala.beans.BeanProperty
Class SimpleClass {
@ BeanProperty var value = 0
}
Call method
Val c = new SimpleClass
C.getValue ()
C.setValue (20)
C.value = 234
Val tmp = c.value
Java code
Private int value = 0
Public int value () {return this.value;}
Public void value_$eq (int Xero1) {this.value = Xero1;}
Public void setValue (int Xero1) {this.value = Xero1;}
Public int getValue () {return value ();}
VI. Main constructor
The first is the Scala code, the constructor of Scala code is more compact and easier to write than java.
Class SimpleClass (name: String) {
}
Call method
Val c = new SimpleClass ("bajie")
Java code
Public class SimpleClass
{
Public SimpleClass (String name)
{
}
}
Here are a few rules that you can do the experiment yourself.
1. If the argument in the constructor has a val or var modifier, it is naturally treated as an attribute in the class, and the get and set methods are generated according to the previous rules
2. If the parameter in the constructor does not have the val and var modifiers, it is just a parameter, but when it is used by at least one method in class, it will be upgraded to a property.
7. From the constructor
There is also a rule that each slave constructor must start with some previous slave constructor or master constructor. In other words, only the main constructor can call the constructor of its parent class. This is different from java.
Class Person (name: String) {
Var name = ""
Var age = 0
Def this (name: String, age: Int) {
This (name)
This.age = age
}
}
8. Inner class, which is similar to the concept of inner class in java, so I won't repeat it here.
At this point, the study on "how to use the Bean attribute of Scala" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.