In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Good programmer big data learning route to share the object of the Scala series 1. Singleton objects do not have static methods and static fields in Scala, but you can use the syntax structure of object to achieve the same purpose
1.scala is similar to the utility class in Java and can be used to store tool functions and constants.
two。 Efficiently share a single immutable instance
3. Singleton mode
Although the singleton object is similar to the utility class in Java, it is not, it is still an object, and the singleton object name can be regarded as a label affixed to the object.
Package logging
/ / define singleton objects using the keyword object
Object Logger {def info (message: String): Unit = println (s "INFO: $message")} the use of singleton objects
/ / Import singleton object information to make it visible in the current class
Import logging.Logger.infoclass Project (name: String, daysToComplete: Int) class Test {val project1 = new Project ("TPS Reports", 1) val project2 = new Project ("Website redesign", 5) / / call the method info ("Created projects") / / Prints "INFO: Created projects"} class defined in the singleton object is that the singleton object cannot take parameters, and the singleton object cannot be instantiated with the new keyword So there is no chance to pass it the instantiated parameters.
The singleton object is initialized the first time it is accessed.
When a singleton object has the same name as a class, it is called the accompanying object of the class, the class and the accompanying object must be defined in a source file, the class is called the accompanying class of the singleton object, and the class and its accompanying objects can access their private members to each other.
Singleton objects that do not share names with accompanying classes are called stand-alone objects and can be used as utility classes for related functions or as entry points for scala applications.
two。 Accompanying objects in Scala classes, objects with the same class name and modified with object are called associated objects. Classes and accompanying objects can access private methods and properties each other, and they must exist in the same source file.
The function of the accompanying object of the class AccountInfo {/ / class is not within the scope of the class / / so you cannot directly call the accompanying object's method var id = AccountInfo.newUniqueNumber ()} object AccountInfo {private var lastNumber = 0 private def newUniqueNumber () = {lastNumber + = 1 with newUniqueNumber (). LastNumber} def main (args: Array [String]) {/ / equivalent to static method call println (AccountInfo.newUniqueNumber ())}} 3. Apply method usually we define the apply method in the accompanying object of the class, when we encounter the class name (parameter 1JI. Parameter n) when the apply method is called
Class AccountInfo {} object AccountInfo {private var lastNumber = 0 private def apply (arg: Int) = {lastNumber = arg*2 + 1; lastNumber} def main (args: Array [String]) {println (AccountInfo (1))} 4. Application object Scala programs must start with the main method of an object, and you can extend the App feature without writing the main method. Object Hello extends App {println ("Hello World")} is identical to object Hello {def main (args: Array [String]): Unit = {println ("Hello World")} 5. Objects with unapply methods in extractors are often used in pattern matching or partial functions.
Import scala.util.Randomobject CustomerID {def apply (name: String) = s "$name--$ {Random.nextLong}" def unapply (customerID: String): Option [String] = {val name = customerID.split ("- -"). Head if (name.nonEmpty) Some (name) else None}} / / call apply method to create an object, which is equivalent to CustomerID.apply ("Sukyoung") val customer1ID = CustomerID ("Sukyoung") / / Sukyoung--23098234908customer1ID match {/ / call unapply method Extract name information case CustomerID (name) = > println (name) / / prints Sukyoung case _ = > println ("Could not extract a CustomerID")}
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.