In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Big data Scala series of characteristics, the definition of characteristics, except for the use of the keyword trait, is no different from the class definition.
Attributes are used to share interfaces or attributes between classes. Both classes and objects can inherit attributes, which cannot be instantiated, so there are no parameters.
Once attributes are defined, you can use extends or with to mix attributes into the class.
1 characteristics used as an interface
Definition of traits:
Trait Logger {
/ / this is an abstract method. Methods that are not implemented in attributes are abstract by default and do not need to be modified by the abstract keyword
Def log (msg:String)
}
The realization of traits by subclasses:
Class ConsoleLogger extends Logger {
/ / override abstract methods without override
Def log (msg:String) {println (msg)}
}
2 with the characteristics of concrete realization
Trait ConsoleLogger {
/ / Note that the interface is different from that in Java
Def log (msg:String) {println (msg)}
}
The use of traits
Class SavingAccount extends Account with ConsoleLogger {
Def withdraw (amount:Double) {
If (amount > balance) log ("Insufficent funds")
Else balance-= amount
}
}
3 objects with characteristics
Scala has its own Logged characteristics, but it has not been implemented.
Trait Logged {
Def log (msg:String) {}
}
If this attribute is used in the class definition
/ / in this class, the log information will not be recorded
Class SavingAccount extends Account with Logged {
Def withdraw (amount:Double) {
If (amount > balance) log ("Insufficent funds")
Else balance-= amount
}
}
Standard ConsoleLogger extends from Logger
Class ConsoleLogger extends Logger {
/ / override abstract methods without override
Def log (msg:String) {println (msg)}
}
You can add this attribute when you create an object:
Val acct1=new SavingAccount with ConsoleLogger
In this way, the same kind of object can be created, but different characteristics can be added.
Val acct2=new SavingAccount with FileLogger
4 multiple superimposed qualities
You can add multiple attributes that call each other to a class or object, and the order in which the attributes are executed depends on the order in which the attributes are added
Trait Logged {
Def log (msg:String)
}
Trait ConsoleLogger extends Logged {
/ / override abstract methods without override
Def log (msg: String) = {println (msg)}
}
/ / time stamp log
Trait TimestampLogger extends ConsoleLogger {
Override def log (msg: String) {
Super.log (s "${java.time.Instant.now ()} $msg")
}
}
/ / truncate overly lengthy log information
Trait ShortLogger extends ConsoleLogger {
Val maxLength = 15
Override def log (msg: String) {
Super.log (
If (msg.length balance) log ("Insufficent funds")
Else balance = balance-amount
}
}
Object test {
Def main (args: Array [String]): Unit = {
Val acct1 = new SavingAccount with ConsoleLogger with TimestampLogger with ShortLogger
Val acct2 = new SavingAccount with ConsoleLogger with ShortLogger with TimestampLogger
Acct1.withdraw (100.0)
Acct2.withdraw (100.0)
}
}
/ / res:
/ / the log method of ShortLogger is executed first, then its super.log calls the log method of TimestampLogger, and finally calls the method of ConsoleLogger to print out the information.
2018-06-15T16:50:28.448Z Insufficent.
/ / first, the log method of TimestampLogger is executed, then its super.log calls the log method of ShortLogger, and finally calls the method of ConsoleLogger to print out the information.
2018-06-15T1.
5 unified programming with special features
Import scala.collection.mutable.ArrayBuffer
Trait Pet {
Val name: String
}
Class Cat (val name: String) extends Pet
Class Dog (val name: String) extends Pet
Val dog = new Dog ("Harry")
Val cat = new Cat ("Sally")
Val animals = ArrayBuffer.empty [Pet]
Animals.append (dog)
Animals.append (cat)
Animals.foreach (pet = > println (pet.name)) / / Prints Harry Sally
The characteristics that Mixins uses for class composition:
Abstract class A {
Val message: String
}
Class B extends A {
Val message = "I'm an instance of class B"
}
/ / trait C here is mixin
Trait C extends A {
Def loudMessage = message.toUpperCase ()
}
Class D extends B with C
Val d = new D
Println (d.message) / / I'm an instance of class B
Println (d.loudMessage) / / I'M AN INSTANCE OF CLASS B
6 characteristics used as a rich interface
/ / pay attention to the combination of abstract methods and concrete methods
Trait Logger {def log (msg: String)
Def info (msg: String) {log ("INFO:" + msg)}
Def warn (msg: String) {log ("WARN:" + msg)}
Def severe (msg: String) {log ("SEVERE:" + msg)}
}
Class Account {
Protected var balance:Double = 0
}
Class SavingsAccount extends Account with Logger {
Def withdraw (amount: Double) {
If (amount > balance) severe ("Insufficient funds") else "you can do this"}
Override def log (msg: String) {println (msg)}
}
Object test {
Def main (args: Array [String]): Unit = {
Val acc = new SavingsAccount
Acc.withdraw (100)
}
}
/ / result
SEVERE: Insufficient funds
7 concrete fields and abstract fields in the characteristics
A field in a trait that has an initial value is concrete, otherwise it is abstract.
Trait ShortLogger extends Logged {
Val maxLength = 15 / / specific field
}
So how does the subclass that inherits this attribute get this field? Scala puts the field directly into a subclass that inherits the specialization, rather than being inherited. For example:
Class SavingsAccount extends Account with ConsoleLogger with ShortLogger {
Var interest = 0.0
Def withdraw (amount: Double) {
If (amount > balance) log ("Insufficient funds")
Else...
}
}
Abstract fields in attributes must be overridden in concrete subclasses:
Trait ShortLogger extends Logged {
Val maxLength: Int// abstract field
Override def log (msg: String) {
Super.log (if (msg.length)
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.