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 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿