Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the object of Scala trait

2025-03-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to use the object of Scala trait". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use Scala trait objects.

1. Trait

Trait Logger {

Def log (msg: String)

}

Class ConsoleLogger extends Logger with Serializable {

Def log (msg: String) {println (msg)}

}

Note that there is no implements, just use extends. In addition, this is actually override, but you don't need to write it. Log is abstract in Logger. If you want to implement multiple trait, you can connect it with with. In addition, like java, one parent class + several Interface

Second, it has the characteristics of concrete realization.

Trait Logger {

Def log (msg: String) {println (msg)}

}

Class Account (val name: String, val password: String) extends Logger {

Def check () = {log ("Account =" + name + "and password =" + password); name = = "bajie" & & password = = "change"}

}

Object Account {

Def main (args: Array [String]) {

Val acc = new Account ("bajie", "change")

Acc.check

}

}

In the previous section, the use of trait is similar to java's interface. So how should it be understood in this section?

Third, objects with trait

Trait Logger {

Def log (msg: String) {}

}

Trait ConsoleLogger extends Logger {

Override def log (msg: String) {println (msg)}

}

Class Account (val name: String, val password: String) extends Logger {

Def check () = {log ("Account =" + name + "and password =" + password); name = = "bajie" & & password = = "change"}

}

Object Account {

Def main (args: Array [String]) {

Val acc = new Account ("bajie", "change")

Acc.check

Val acc1 = new Account ("bajie", "change") with ConsoleLogger

Acc1.check

}

}

Some more changes have been made, Logger.log has an empty implementation, ConsoleLogger.log has done the work, Account is mixed with Logger, so acc.check does not do the output work, while val acc1 = new Account ("bajie", "change") with ConsoleLogger, acc1.check has output.

Well, at present, I can't figure out exactly what kind of work such a design should be used to accomplish, but it must be useful, try to remember it.

Fourth, the superposition of traits (very cool)

Trait Logger {

Def getLog: String = "I am the root;"

}

Trait ConsoleLogger extends Logger {

Override def getLog = super.getLog + "Something happend"

}

Trait TimeLogger extends Logger {

Override def getLog = super.getLog + "at" + new Date () + ""

}

Class Account (val name: String, val password: String) extends Logger {

Def check () = {println (getLog); name = = "bajie" & & password = = "change"}

}

Object Account {

Def main (args: Array [String]) {

Val acc1 = new Account ("bajie", "change") with TimeLogger with ConsoleLogger

Acc1.check

Val acc2 = new Account ("bajie", "change") with ConsoleLogger with TimeLogger

Acc2.check

}

}

Finally, there is output.

I am the root

I am the root; at Fri Aug 12 01:32:24 PDT 2016 Something happend

I am the root; Something happend at Fri Aug 12 01:32:24 PDT 2016

Feel it, I don't know!

5. Interface + abstract class

Trait Logger {

Def log (message: String)

Def info (message: String) {println (message)}

}

Class Account extends Logger {

Var accName = "bajie"

Var accPass = "change"

Def check () = {info ("a user login"); accName = = "bajie" & & accPass = = "change"}

Def log (message: String) {println (message)}

}

Here Logger is both an interface and an abstract class. Account can use the info method directly, but it must also implement the log method itself.

VI. Concrete attributes and abstract attributes

Trait Logger {

Def log (message: String)

Def info (message: String) {println (message)}

Val logLevel: Int

Val maxlength = 20

}

Class Account extends Logger {

Var accName = "bajie"

Var accPass = "change"

Def check () = {info ("a user login"); accName = = "bajie" & & accPass = = "change"}

Def log (message: String) {println (message)}

Val logLevel = 3

}

Similar to the previous section, properties can be divided into concrete and abstract. Specific fields can be used directly in Account, and abstract fields must be specified.

At this point, I believe you have a deeper understanding of "how to use Scala trait objects". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report