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 > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what is the Trait construction mechanism 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 how the Trait construction mechanism of Scala is.
The Construction Mechanism of Trait
People with a java background are well aware of how java defines constructors and inheritance. In scala, inheritance is somewhat similar to java. However, the definition of construction method is different, it should be said that it is still very different. In java, to define a constructor is to define a method with the same name as the class that does not return a value type.
Trait also has its own constructor mechanism, but unlike classes, it cannot define the constructor that receives parameters, only the default constructor. In Trait, the code that is not included in any method is the constructor part.
The constructor that inherits the Trait constructor is called in the following order:
1. Execute the parent constructor first
two。 Then execute the constructor of trait. If multiple trait classes are inherited, they are executed in the order from left to right. When constructing trait, the parent trait is constructed first. If multiple trait inherits from the same trait, the parent trait executes only once.
3. Finally, the constructor of the subclass is executed
The following code is used in practice:
Base class Person
Parent Trait: Logger
Both child Trait:Logger1,Logger2 inherits from Logger
Subclass Student5 inherits subclass Person inherits Trait: Logger1 first and then Logger2
Code example:
Person class:
Package com.hadoop.ljs.spark.study.TraitTest/** * @ author: Created By lujisen * @ company ChinaUnicom Software JiNan * @ date: 2020-02-15 19:25 * @ version: v1.0 * @ description: com.hadoop.ljs.spark.study.TraitTest * / class Person {/ * does not belong to any method here belongs to the constructor part * / println ("Person Constructor")}
Parent class and Trait code examples:
Package com.hadoop.ljs.spark.study.TraitTest/** * @ author: Created By lujisen * @ company ChinaUnicom Software JiNan * @ date: 2020-02-15 19:26 * @ version: v1.0 * @ description: com.hadoop.ljs.spark.study.TraitTest * / trait Logger {/ * does not belong to any method here belongs to the constructor part * / println ("Trait Logger Constructor")} trait Logger1 extends Logger {/ * No Belongs to any method here belongs to the constructor part * / println ("Trait Logger1 Constructor")} trait Logger2 extends Logger {/ * does not belong to any method, here belongs to the constructor part * / println ("Trait Logger2 Constructor")} class Student5 extends Person with Logger1 with Logger2 {/ * does not belong to any method, here belongs to the constructor part * / println ("Class Student5 Constructor")}
Main function test class:
Package com.hadoop.ljs.spark.study.TraitTest/** * @ author: Created By lujisen * @ company ChinaUnicom Software JiNan * @ date: 2020-02-15 19:30 * @ version: v1.0 * @ description: com.hadoop.ljs.spark.study.TraitTest * / object TraitConstructorTest {def main (args: Array [String]): Unit = {val student5=new Student5}}
Output result:
Person ConstructorTrait Logger ConstructorTrait Logger1 ConstructorTrait Logger2 Constructor Class Student5 Constructor
The class construction first constructs the parent class; the Trait construction parent Trait,Logger1 and Logger2 are all inherited from Logger only once, and the output here is only once; in the case of multi-inheritance, the Trait construction order is from left to right, first Logger1, then Logger2, and finally main class construction.
Thank you for your reading, the above is the content of "what is the Trait construction mechanism of Scala". After the study of this article, I believe you have a deeper understanding of how the Trait construction mechanism of Scala is, 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.