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/02 Report--
This article mainly explains the "introduction of companion classes and companion objects of Scala". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the introduction of companion classes and companion objects of Scala".
Scala is a multi-paradigm programming language. Multi-paradigm means multiple programming methods. What are the programming methods? At present, there are four programming methods: process-oriented, object-oriented, generic and functional.
When the singleton object has the same name as the class, the singleton object is called the companion object of the class, and the class is called the companion class of the singleton object. Companion classes and companion objects are defined in the same source file, and companion objects and companion classes can access each other's private members. A singleton object that does not have the same name as the accompanying class is called an orphaned object.
The entry of the scala object program is definitely ojbect Name {def main (args: array [string]) {statement to be executed}}
Look at the example:
Import scala.collection.mutable.Mapclass ChecksumAccumulator {private var sum = 0 def add (b: Byte) {sum + = b} def checksum (): Int = ~ (sum & 0xFF) + 1} object ChecksumAccumulator {privateval cache = Map [String Int] () def calculate (s: String): Int = if (cache.contains (s)) cache (s) else {val acc = new ChecksumAccumulator for (c cs) println ("s:" + s + "cs:" + cs) cs} def main (args: Array [String]) {println ("Java 1:" + calculate ("Java") println ("Java 2:" + calculate ("Java")) Println ("Scala:" + calculate ("Scala"))}}
The ChecksumAccumulator singleton object has a method, calculate, which is used to calculate the checksum of the characters in the String parameter it takes. It also has a private field, cache, a mutable mapping that caches the previously calculated checksum. The first line of the 2 method, "if (cache.contains (s))", checks the cache to see if the passed string already exists as a key in the mapping. If so, only the mapped value, "cache (s)", is returned. Otherwise, the else clause is executed and the checksum is calculated. The first line of the else clause defines a val called acc and initializes it with the newly created ChecksumAccumulator instance. The next line is a for expression that loops through each character of the incoming string and calls toByte to convert the character to Byte, which is then passed to the add method of the ChecksumAccumulator instance that acc refers to. After completing the for expression, the method on the next line calls checksum on acc, gets the checksum of the incoming string, and stores it in a val called cs. The next line, "cache + = (s-> cs)", maps the passed string key to the checksum value of the integer and adds this key-value pair to the cache mapping. The last expression of the method, "cs", guarantees that the checksum is the result of this method.
The printed result here is:
S:Java cs:-130Java 1:-130Java 2:-130s:Scala cs:-228Scala:-228
The problem is that ChecksumAccumulator singletons cannot be new, but val acc = new ChecksumAccumulator appears in the code. Isn't that a contradiction? In fact, the new is actually the accompanying class of the ChecksumAccumulator singleton object, that is, the ChecksumAccumulator class, and the accompanying class and the accompanying object can access each other's private members, so acc can access the cache variable of the ChecksumAccumulator singleton object. Similarly, the first time you test the "Java" string, the acc instance executes the checksum () method, and then stores this data in cache, the val. The second time you test the "Java" string, the program gets the data directly from the cache variable and returns it.
You can also see that one of the differences between a class and a singleton object is that the singleton object is initialized on the first visit and cannot be new with parameters, while the class can be new and can take parameters. Each singleton object is implemented as an instance of a fictional class pointed to by a static variable: synthetic class, so they have the same initialization syntax as the Java static class.
Thank you for your reading, the above is the content of "introduction of companion classes and companion objects of Scala". After the study of this article, I believe you have a deeper understanding of the introduction of companion classes and companion objects of Scala, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.