scala特性
Scala Traits consists of method and field definitions that can be reused by mixing classes. The class can mix any number of traits.
Scala特性包括方法和字段定义,可以通过混合类重用 。 该类可以混合任意数量的特征。
- Traits define the objects by specifying the signature of the supported methods. 特性通过指定受支持方法的签名来定义对象。
- Traits are declared in the same way as class except that the keyword trait is used. 除了使用关键字trait以外,以与类相同的方式声明特征。
For example consider a trait without any method implementation below.
例如,考虑以下没有任何方法实现的特征。
trait Cardetails{
def details(d:String):String
}
class Cardet extends Cardetails {
import scala.io.Source
override def details(source:String) = {
Source.fromString(source).mkString
}
}
object car {
def main(args:Array[String]){
val c1 = new Cardet
println(c1.details("Car details are being displayed"))
println(c1.isInstanceOf[Cardetails])
}
}
Below image shows the execution and output in scala shell.
下图显示了scala shell中的执行和输出。
We are defining a trait Cardetails
without any method implementation. In the class Cardet
we are overriding the method details and then creating an object car.
我们正在定义特征Cardetails
而无需任何方法实现。 在Cardet
类中,我们将重写方法详细信息,然后创建一个对象车。
Consider an example for a trait with method implementation;
考虑一个带有方法实现的特征的例子。
import scala.io.Source
trait detcar{
def readdetails(d:String):String =
Source.fromString(d).mkString
}
class Car(var cname:String, var cno:Int){
def details = cname+" "+cno
}
class Alto( cname:String, cno:Int,var color:String) extends Car(cname,cno) with detcar{
override def details = {
val det = readdetails(color)
cname+"\n"+cno+"\n"+"Color:"+color
}
}
object cartest {
def main(args:Array[String]){
val a1 = new Alto("Alto",34,"Black")
println(a1.details)
}
}
Below is the output produced when we execute main method.
下面是执行main方法时产生的输出。
scala> cartest.main(null)
Alto
34
Color:Black
We are defining the trait cardet with the method “readdetails” having the implementation. In the class car we are defining “details” with cname and cno. We are declaring the class Alto which extends car class and implements the trait cardet with an extra information about car color. We are creating cartest object and invoking the Alto class passing the name number and color and printing the details.
我们正在使用具有实现方式的“ readdetails”方法定义特征卡片。 在类车中,我们使用cname和cno定义“详细信息”。 我们宣布的是Alto类,它扩展了汽车类,并使用有关汽车颜色的附加信息实现了特征卡。 我们正在创建cartest对象,并调用传递名称编号和颜色的Alto类并打印详细信息。
Usage of Traits
性状的运用
While implementing a reusable collection or behavior, we have to decide between a trait and abstract class. There are some of the guidelines that can be followed.
在实现可重用的集合或行为时,我们必须在特征和抽象类之间做出选择。 可以遵循一些准则。
- If efficiency is the key criteria then it is advisable to use class. Traits get compiled to interfaces and therefore may pay a slight performance overhead. 如果效率是关键标准,那么建议使用类。 特性被编译到接口,因此可能会付出一些性能开销。
- If you need to inherit from java code then better use abstract classes as traits do not have Java analog it would be strange to inherit from a trait. 如果您需要从Java代码继承,那么最好使用抽象类,因为特征没有Java模拟,从特征继承是很奇怪的。
- Concrete class would be recommended if the behavior is not reusable. 如果行为不可重用,则建议使用具体类。
- Trait can be used if the behavior is reused in multiple, unrelated classes. 如果在多个不相关的类中重复使用该行为,则可以使用特质。
That’s all for a quick roundup on Scala traits, we will look into more scala features in coming posts.
这是对Scala特性的快速汇总,我们将在以后的文章中研究更多的scala功能。
翻译自: https://www.journaldev.com/8153/scala-traits-example-tutorial
scala特性