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 > Servers >
Share
Shulou(Shulou.com)06/02 Report--
1. Object is equivalent to a single instance of class, so the variables declared in object are static variables, static methods
2. Variables declared in object are global variables, that is, public properties of the class, or properties similar to those of the parent class in java
3. The constructor in object, that is, the code that is not in method inside object, belongs to the content of constructor.
4. Again, the main constructor of object will only execute on the first call, multiple calls, only once.
Object usage scenario
1. Singleton mode
2. Tool class
Companion object and concomitant class?
If there is a class and an object with the same name as class, then the object is called the companion object of class, and class is the companion class of object.
Request:
Accompanying classes and objects must be stored in a .scala file
Characteristics?
/ / compared with ordinary private attributes in concomitant objects and associated classes?
/ / concomitant objects, the scope of use of private attributes in accompanying classes has been expanded.
/ / Associated objects and accompanying classes can access each other's private properties, while ordinary private properties can only be used in their own classes.
Package com.xej.learning.objectLearning/** * objectLearning Test * / private [objectLearning] class Kafka (val name: String) {/ / purpose: when testing multiple calls to object Its main constructor calls def sayHello () several times: Unit = {objectTest.name println ("-")} object objectTest {val name = "spark" println ("- object---- main constructor -") def main (args: Array [String]): Unit = { New Kafka ("spark") .sayHello ()}
The implementation results are as follows:
-object---- main constructor-
Test case 2:
Package com.xej.learning.objectLearning/** * main testing companion object Access range of private attributes in the accompanying class * / private [objectLearning] class Student (val name: String, val age: Int Address: String= "xizhimen") {privateval sex = "male" val weight = 80 def sayHello (): Unit = {Student.address = address / / access the normal and private attributes in the accompanying object println ("name:\ t" + name + "-->" + "age:\ t" + age + "\ naddress:\ t" + Student.address + "- > school) :\ t "+ Student.school)}} object Student {/ / private attributes in concomitant objects are compared to ordinary private attributes? / / concomitant objects, the scope of use of private attributes in accompanying classes has been expanded. / / accompanying objects and accompanying classes can access each other's private properties, while ordinary private attributes can only be called in their own classes, privateval school = "bjtu" / / can only be called in associated classes. Var address = "beijing" / / can also be called in other places except in accompanying classes. Same as normal attributes / / access the attribute val stu = new Student in the associated class ("lisi", 22 "haidian") println ("- access-attribute-name:\ t" + stu.name) println ("- access-private attribute-sex:\ t" + stu.sex) println ("- access-attribute-- weight:\ t in the associated class "+ stu.weight)} object objectTest2 {def main (args: Array [String]): Unit = {/ / call the public attribute println ("-address:\ t "+ Student.address) / / school is the private attribute of the companion object / / println ("- school:\ t" + Student.school) new Student ("zhansan", 19) .sayHello () new Student ("xiaoming", 22, "haidian") .sayHello ()}
Test results:
-access-attribute-- name: lisi---- access-Private attribute-- sex: male---- access-attribute-- weight: 80----address: beijingname: zhansan-> age: 19address: Xizhimen-> school: bjtuname: xiaoming-- > age: 22address: haidian-> school: bjtuProcess finished with exit code 0
In short:
When you see concomitant objects, accompanying classes, think of private attributes immediately
The use of concomitant objects in the scene, that is, what the accompanying objects are used, so that it is helpful to understand the marathon source code, spark source code is not confused.
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.