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
热门推荐
1 如何训练狗狗不叫
2 训练狗狗不叫
3 训练狗狗随行
4 怎么训练狗狗不叫
7 训练比熊狗狗教程
10 上海狗狗训练
11 怎么训练狗狗上厕所
12 训练狗狗趴下
13 狗狗服从训练
14 训练狗狗拒食
15 训练狗狗笑
16 训练狗狗大便
17 训练狗狗立
18 训练狗狗捡东西