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.Sourceoverride def details(source:String) = {Source.fromString(source).mkString}
}object car {def main(args:Array[String]){val c1 = new Cardetprintln(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.Sourcetrait 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.

在实现可重用的集合或行为时,我们必须在特征和抽象类之间做出选择。 可以遵循一些准则。

  1. 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.如果效率是关键标准,那么建议使用类。 特性被编译到接口,因此可能会付出一些性能开销。
  2. 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模拟,从特征继承是很奇怪的。
  3. Concrete class would be recommended if the behavior is not reusable.如果行为不可重用,则建议使用具体类。
  4. 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特性

scala特性_Scala特性示例教程相关推荐

  1. scala数据类型_Scala数据类型示例教程

    scala数据类型 A data type tells the compiler about the type of the value to be stored in a location. The ...

  2. scala特性_Scala | 特性应用

    scala特性 特性应用 (Trait App) Scala uses a trait called "App" which is used to convert objects ...

  3. scala 连接符_Scala标识符示例教程

    scala 连接符 The names of variables, classes, objects and methods are collectively called Identifiers. ...

  4. scala if 语句缩写_Scala If-Else语句示例教程

    scala if 语句缩写 Scala If-Else statement is a decision making statement which executes the conditional ...

  5. scala迭代器_Scala选项和迭代器示例

    scala迭代器 Scala Option can be defined as container that holds zero or more elements of the given type ...

  6. mysql query browswer_MySQL数据库新特性之存储过程入门教程

    MySQL数据库新特性之存储过程入门教程 在MySQL 5中,终于引入了存储过程这一新特性,这将大大增强MYSQL的数据库处理能力.在本文中将指导读者快速掌握MySQL 5的存储过程的基本知识,带领用 ...

  7. scala特质_Scala的特质

    scala特质 Scala特质 (Scala traits) Traits in Scala are like interfaces in Java. A trait can have fields ...

  8. 【一天一门编程语言】Scala 语言程序设计极简教程

    Scala 语言程序设计极简教程 用 markdown 格式输出答案. 不少于3000字.细分到2级目录. 目录 简介 基本语法 变量 数据类型 运算符 流程控制 函数 类 应用 安装 环境搭建 编程 ...

  9. scala设计模式_Scala中的工厂设计模式

    scala设计模式 I'm going to deliver a series of Scala Design Patterns in my coming posts. I will pick-up ...

最新文章

  1. [工具推荐]用了TrueCrypt 再无难掩之隐
  2. php error log 函数,php日志函数error_log如何使用 php日志函数error_log用法介绍
  3. 分享我的学习记录 svn地址
  4. [云炬创业基础笔记] 第三章测试4
  5. ubuntu16.04安装R及RStudio
  6. 计算平面坐标某点(x,y)与原点(0,0)的角度
  7. 个人编程思想理解的片段(主要是面向对象的,参考了许多人的观点,还不成熟,没形成系统,请大家多指点)...
  8. 计算不定方程解的个数的方法汇总
  9. win10安装时有个修复计算机,win10出现故障,在安全模式下安装Windows更新,彻底修复问题...
  10. 验证码类库CaptchaMvc
  11. 鸟瞰 Java 并发框架
  12. 设计模式之抽象工厂模式(Java实现)
  13. php 图片上加文字,PHP实现在图片中添加中文文字
  14. 好听的摇滚_好听的摇滚歌曲有哪些 十大最好听中国摇滚歌曲
  15. 大厂前端面试考什么?
  16. JS鼠标移入移出事件:onmouseover事件和onmouseout事件实例
  17. Openpose Windows10环境并且是3系显卡+ Unity 环境部署
  18. 使用Arduino Motor Shield控制步进制电机
  19. xml生成xsd文件的方法
  20. Java获取unix时间戳

热门文章

  1. 各数据类型的取值范围及占用字节数。。
  2. inner/left/right inner
  3. Split的使用(C#)
  4. 以命令行的格式读取音频文件信息,并将读取的内容写到输出文件中
  5. [转载] Python使用list.reverse()返回None
  6. 解决filezilla中无法显示中文的文件名
  7. LabView学习笔记(二):滤波器实验
  8. mysql命令成功数值不变_MySQL基础知识精华汇总_3(mysql基础命令操作)
  9. OpenCV图像处理(17)—— 各种二值化对比
  10. java 上界和下界,Java 泛型上下界(上下限)