In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to mix a specific Trait into a class in Scala". In daily operation, I believe that many people have doubts about how to mix a specific Trait into a class in Scala. The editor consulted all kinds of materials and sorted out a simple and useful method of operation. I hope it will be helpful for you to answer the doubt of "how to mix a specific Trait into a class in Scala". Next, please follow the editor to study!
Mix a specific Trait for a specific instance of a class
Trait is mixed for one instance object of the class, while other instance objects of the class do not inherit the corresponding Tarit.
Here, first create a new Trait class BaseSayHello,SayHello (inheriting BaseSayHello)
The test class Student4 is specified to inherit from BaseSayHello, code example:
Package com.hadoop.ljs.spark.study.TraitTest/** * @ author: Created By lujisen * @ company ChinaUnicom Software JiNan * @ date: 2020-02-15 15:18 * @ version: v1.0 * @ description: com.hadoop.ljs.spark.study.TraitTest * / trait BaseSayHello {def printHello (): Unit = {println ("hello, i am BaseSayHello")}} trait SayHello extends BaseSayHello {override def printHello (): Unit = {println ("hello") I am SayHello ")} class Student4 (name:String) extends BaseSayHello {def printName (): Unit = {println (" my name is "+ name) printHello ()}}
The main function test class Student4TraitTest. Here, two new instance objects student1,student2 are created. Note that if the two instantiation methods are different, the parent Trait class function called is different. Here, only the student2 instance class can call SayHello's function printHello (), while other instance classes all call BaseSayHello's function printHello ():
Package com.hadoop.ljs.spark.study.TraitTest/** * @ author: Created By lujisen * @ company ChinaUnicom Software JiNan * @ date: 2020-02-15 15:51 * @ version: v1.0 * @ description: com.hadoop.ljs.spark.study.TraitTest * / object Student4TraitTest {def main (args: Array [String]): Unit = {val student1=new Student4 ("ljs1") / * specify the integration Trait*/ val student2=new Student4 ( "ljs2") with SayHello / * call the method of BaseSayHello * / student1.printName () / * to specify the inheritance SayHello The printHello () method of SayHello * / student2.printName ()}} will be called here. The study on "how to mix a specific Trait into a class in Scala" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.