1 PackageCom.swust.example2 3Object TraitDemo2extendsapp{4 5 //Abstract class6 Abstract classWriter {7 def writemessage (message:string)8 }9 /**Ten * Inherit this abstract class, and call this abstract method with super One * Scala requires that methods be declared as abstract override A * Override tells Scala to provide an implementation for a known method of the base class - * Abstract indicates that the actual final implementation of this method is provided by the class that mixes this trait - */ theTrait Uppercasewriterextendswriter{ - AbstractOverride Def writemessage (message:string) ={ - Super. Writemessage (message.touppercase) - } + } - /** + * Two things to call Super.writemessage,scala A * 1. Delay binding for this call at * 2. The class required to mix these trait provides the implementation of this method - */ -Trait Wordfilterwriterextendswriter{ - AbstractOverride Def writemessage (message:string) ={ - Super. Writemessage (Message.replace ("stupid", "s-----")) - } in } - classStringwriterdelegateextendswriter{ toVal writer =NewJava.io.StringWriter + //implement the method of the abstract class without the override -def writemessage (message:string) =writer.write (message) theOverride Def toString (): String =writer.tostring * } $ //mix trait When creating an instancePanax NotoginsengVal First =Newstringwriterdelegate with Uppercasewriter with Wordfilterwriter -Val last =Newstringwriterdelegate with Wordfilterwriter with Uppercasewriter theFirst.writemessage ("There is no sin except stupidity") +Last.writemessage ("There is no sin except stupidity") A println (first) the println (last) + -}
The delayed binding of Scala learning