scala java混合
Scala | 特性混合 (Scala | Trait Mixins )
In Scala, the number of traits can be extended using a class or an abstract class. This is known as Trait Mixins. For extending, only traits, the blend of traits, class or abstract class are valid.
If the sequence of Trait Mixins is not maintained, an error is thrown by the compiler. It is used in composing a class. As multiple traits can be inherited.
在Scala中,可以使用類或抽象類擴展特征的數量。 這被稱為特質混合 。 對于擴展,只有特征,特征,類或抽象類的混合才有效。
如果未保留“ 特性混合”的順序,則編譯器將引發錯誤。 它用于組成一個類。 由于可以繼承多個特征。
Let's look at a few examples to understand the topic better,
讓我們看一些例子,以更好地理解該主題,
Example 1: Extending abstract class with a trait
示例1:使用特征擴展抽象類
trait Bike {
def Bike() ;
}
abstract class Speed {
def Speed() ;
}
class myBike extends Speed with Bike {
def Bike() {
println("Harley Davidson Iron 883") ;
}
def Speed() {
println("Max Speed : 170 KmpH") ;
}
}
object myObject {
def main(args:Array[String]) {
val newbike = new myBike() ;
newbike.Bike() ;
newbike.Speed() ;
}
}
Output
輸出量
Harley Davidson Iron 883
Max Speed : 170 KmpH
Example 2: Extending abstract class without a trait
示例2:擴展不帶特征的抽象類
trait Bike {
def Bike() ;
}
abstract class Speed{
def Speed() ;
}
class myBike extends Speed {
def Bike() {
println("Harley Davidson Iron 883") ;
}
def Speed() {
println("Max Speed : 170 KmpH") ;
}
}
object myObject {
def main(args:Array[String]) {
val newbike = new myBike() with Bike;
newbike.Bike() ;
newbike.Speed() ;
}
}
Output
輸出量
Harley Davidson Iron 883
Max Speed : 170 KmpH
翻譯自: https://www.includehelp.com/scala/trait-mixins.aspx
scala java混合