Scala特性混合
Scala|特性混合
在Scala中,可以使用类或抽象类来扩展特征的数量。这被称为特质混合。对于扩展,只有特征,特征,类或抽象类的混合才有效。
如果未保留“特性混合”的顺序,则编译器将引发错误。它用于组成一个类。由于可以继承多个特征。
让我们看一些例子,以更好地理解该主题,
示例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() ;
}
}输出结果
Harley Davidson Iron 883 Max Speed : 170 KmpH
示例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() ;
}
}输出结果
Harley Davidson Iron 883 Max Speed : 170 KmpH
热门推荐
10 祝孙辈生日祝福语简短
11 恋人跨年祝福语大全简短
12 考大学敬酒简短祝福语
13 开工投产大吉祝福语 简短
14 偶然结婚祝福语朋友简短
15 感谢学长学姐祝福语简短
16 戏精的祝福语简短
17 我要结婚祝福语简短
18 婚前女儿生日祝福语简短