In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the example analysis of Trait in Scala, which is very detailed and has certain reference value. Friends who are interested must finish it!
In Scala, Trait is a special concept. First, Trait can be used as an interface, where the interface of Trait is similar to that of Java. At the same time, abstract methods can be defined in Trait, which, like abstract methods in abstract classes, do not give the concrete implementation of the method.
In general, the class of Scala can only inherit a single parent class, but if it is a Trait (feature), it can inherit more than one. As a result, multiple inheritance is implemented. Trait (feature) is defined in a similar way to a class, but it uses the keyword trait, as shown below:
Trait Equal {def isEqual (x: Any): Boolean def isNotEqual (x: Any): Boolean =! isEqual (x)}
The above Trait consists of two methods: isEqual and isNotEqual. The isEqual method does not define the implementation of the method, but isNotEqual defines the implementation of the method. Subclass inheritance features can implement methods that have not been implemented. So Scala Trait is actually more like an abstract class of Java.
The following demonstrates a complete example of a feature:
Trait Equal {def isEqual (x: Any): Boolean def isNotEqual (x: Any): Boolean =! isEqual (x)} class Point (xc: Int, yc: Int) extends Equal {var x: Int = xc var y: Int = yc def isEqual (obj: Any) = obj.isInstanceOf [Point] & & obj.asInstanceOf [point] .x = = x} object Test {def main (args: Array [String]) {val p1 = new Point (2,3) val p2 = new Point (2) 4) val p3 = new Point (3,3) println (p1.isNotEqual (p2)) println (p1.isNotEqual (p3)) println (p1.isNotEqual (2))}}
Execute the above code, and the output is as follows:
$scalac Test.scala$ scala Testfalsetruetrue feature construction sequence
Features can also have constructors, which are made up of initialization of fields and statements in other features. These statements are executed when any object mixed with the feature is constructed.
The execution order of the constructor:
1. Call the constructor of the superclass; 2. The feature constructor is executed after the superclass constructor and before the class constructor; 3. Features are constructed from left to right; 4. In each feature, the parent feature is constructed first; 5. If multiple features share a parent feature, the parent feature will not be reconstructed 6. All features are constructed and subclasses are constructed.
The order of the constructor is the reverse of the linearization of the class. Linearization is a technical specification that describes all supertypes of a type.
The above is all the content of the article "sample Analysis of Trait in Scala". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.