In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Big data, a good programmer, shares scala singletons and concomitant objects along the learning route.
Scala singleton
Object SingletonDemo {
Def main (args: Array [String]): Unit = {
Val s = SessionFactory
Println (s.getSession)
Println (s.getSession.size) / / .size gets several session objects
}
}
Object SessionFactory {
Println ("SessionFactory is executed")
/ / counter
Var I = 5
/ / an array of session objects
Val sessions = new ArrayBuffer [Session] ()
While (I > 1) {
Println ("while is executed")
Sessions.append (new Session)
I-= 1
}
/ / get the session object
Def getSession = sessions
}
Class Session {
}
Associated object
Singleton objects include concomitant objects, and classes and companion objects can access each other, even private fields and properties modified by private
The concomitant object is first of all a singleton object, which is defined by object.
In scala, there are two kinds of singleton objects
1. An association is not automatically associated with a singleton object of a particular class, called a stand-alone object-> Standalone Object
two。 A singleton object associated with a class that has the same class name as the class and is called accompanying object-> Companion Object
Class companionObject {
Var id = 0
Privateval name = "zhaoxiaodan"
Def printContent () = {
Println (name+companionObject.Constant)
}
}
Object companionObject {
Privateval Constant = "is my goddess"
Def main (args: Array [String]): Unit = {
Val co = new companionObject
Co.id = 8
Println (co.id)
Println (co.name)
Co.printContent () / / zhaoxiaodan is my goddess
}
}
Apply and unapply (usually define the apply method in the companion object of the class)
The apply method, often referred to as the injection method, does an initialization operation in the accompanying object of the class
The parameter list of the apply method does not need to be unified with the main constructor list
The unapply method is usually an extraction method, and the unapply method can be used to extract a fixed number of objects and values in the constructor.
The unapply method returns an Option, and if there are values in the Option, there is an internal some object that encapsulates these values.
Class ApplyDemo (val name: String, val age: Int, val faceValue: Int) {
}
Object ApplyDemo {
/ / injection method
Def apply (name: String, age: Int): ApplyDemo = {
New ApplyDemo (name,age,faceValue = 80) / / initialization
}
/ / extraction method
Def unapply (applyDemo: ApplyDemo): Option [(String,Int,Int)] = {
If (applyDemo = = null) {
None
} else {
Some (applyDemo.name,applyDemo.age,applyDemo.faceValue)
}
}
}
Object ApplyTest {
Def main (args: Array [String]): Unit = {
Val applyDemo = ApplyDemo (Zhao Xiaodan, 18)
ApplyDemo match {
Case ApplyDemo (Zhao Xiaodan, age,faceValue) = > println (s "name: Zhao Xiaodan, age:$age,fv:$faceValue")
Case _ = > println ("is null")
}
}
}
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.