scala特质

Scala特质 (Scala traits)

Traits in Scala are like interfaces in Java. A trait can have fields and methods as members, these members can be abstract and non-abstract while creation of trait.

Scala中的特性类似于Java中的接口 。 特征可以具有作为成员的字段和方法,这些成员在特征创建时可以是抽象的且非抽象的。

The implementation of Scala traits can implement a trait in a Scala Class or Object.

Scala特征实现可以在Scala类或对象中实现特征。

Some features of Scala traits:

Scala特征的一些特征:

  • Members can be abstract as well as concrete members.

    成员可以是抽象成员,也可以是具体成员。

  • A trait can be extended by another trait.

    一个特性可以被另一个特性扩展。

  • Made using "trait" keyword.

    使用“ trait”关键字制成。

Syntax:

句法:

    trait trait_name{
def method()
}

Example showing the use of trait

示例显示特质的使用

This example is taken from https://docs.scala-lang.org/tour/traits.html

此示例取自https://docs.scala-lang.org/tour/traits.html

trait Iterator[A] {def hasNext: Boolean
def next(): A
}
class IntIterator(to: Int) extends Iterator[Int] {private var current = 0
override def hasNext: Boolean = current < to
override def next(): Int = {if (hasNext) {val t = current
current += 1
t
} else 0
}
}
val iterator = new IntIterator(10)
iterator.next()  // returns 0
iterator.next()  // returns 1

This example shows the use of trait and how it is inherited? The code creates a trait name Iterator, this trait there are 2 abstract methods hasNext and next. Both the methods are defined in the class IntInterator which defines the logic. And then creates objects for this class to use the trait function.

这个例子展示了特质使用以及它是如何被继承的? 该代码创建一个特征名称Iterator ,此特征有2个抽象方法hasNext和next 。 这两种方法都在定义逻辑的IntInterator类中定义。 然后创建此类的对象以使用trait函数

Another working example,

另一个工作示例

trait hello{def greeting();
}
class Ihelp extends hello {def greeting() {println("Hello! This is include Help! ");
}
}
object MyClass {def main(args: Array[String]) {var v1 = new Ihelp();
v1.greeting
}
}

Output

输出量

Hello! This is include Help!

This code prints "Hello! This is include Help!" using trait function redefinition and then calling that function.

该代码显示“您好!这包括帮助!”。 使用特征函数重新定义 ,然后调用该函数。

Some pros and cons about using traits

关于使用特质的一些利弊

  • Traits are a new concept in Scala so they have limited usage and less interoperability. So, for a Scala code That can be used with a Java code should not use traits. The abstract class would be a better option.

    特质是Scala中的一个新概念,因此用途有限且互操作性较低。 因此,对于可与Java代码一起使用的Scala代码,不应使用特征。 抽象类将是一个更好的选择。

  • Traits can be used when the feature is to be used in multiple classes, you can use traits with other classes too.

    性状时,可以使用该功能是在多个类别中使用,可以使用与其他类的特征了。

  • If a member is to be used only once then the concrete class should be used rather than a Traits it improves the efficiency of the code and makes it more reliable.

    如果一个成员仅使用一次,则应使用具体的类而不是Traits,这将提高代码的效率并使其更可靠。

翻译自: https://www.includehelp.com/scala/traits-in-scala.aspx

scala特质

scala特质_Scala的特质相关推荐

  1. Scala(三) 类和特质

    一:基础语法 一个.scala文件可以定义多个类 一个.scala文件可以定义多个类(class.object.trait), class和object定义的类名可以一直,如果object的类名和cl ...

  2. Scala特质讲解【特质继承形式、对象混入特质、特质适配器模式,模板方法模式,职责链模式、trait的构造机制、特质继承类】

    文章目录 特质 类继承单个特质 类继承多个特质 object继承特质trait 演示特质中的成员 对象混入特质 使用trait实现适配器模式 使用trait实现模板方法模式 使用trait实现职责链模 ...

  3. Scala中的trait特质

    Trait字面意思就是特质或者特征,这个词翻译成特征比较合适.它的意义和Java,c#中的接口很类似.但是trait支持部分实现,也就是说可以在scala的trait中实现部分方法. 在下面的例子中定 ...

  4. scala 基础十一 scala 中的trait特质

    1.Scala类型系统的基础部分是与Java非常相像的.Scala与Java一样有单一的根类,Java通过接口来实现多重继承,而Scala则通过特征(trait)来实现(Scala的特征可以包含实现代 ...

  5. Scala父类和父特质的执行顺序

    总结

  6. java系统架构师有的特质_Java中特质模式的定义

    java系统架构师有的特质 在本文中,我将介绍特征的概念,并为您提供一个具体示例,说明如何在Java中使用它们以减少对象设计中的冗余. 我将首先提出一个虚构的案例,其中可以使用特征来减少重复,然后以使 ...

  7. scala特性_Scala特性示例教程

    scala特性 Scala Traits consists of method and field definitions that can be reused by mixing classes. ...

  8. scala运算符_Scala的所有符号运算符是什么意思?

    scala运算符 Scala的符号运算符 (Scala's symbolic operators) The symbolic operators in Scala are symbols that h ...

  9. java scala 互操作_Scala类型边界和Java通用互操作

    嗯,你正在踩踏Scala中使用的Java泛型的方差问题.我们一步一步走吧. 我们来看看你的实现: // does not compile (with your original error) def ...

最新文章

  1. erlang精要(30)-卫语句
  2. .NET Core 实例接口代理转发
  3. java的集合应用_Java之集合类应用总结
  4. maven编译报程序包不存在_Hadoop学习之路(二)Hadoop2.7.5在CentOS6.7上的编译
  5. oracle10g下载与安装教程,Oracle10g安装图解与Oracle10g卸载教程
  6. 【HenCoder Plus】第四期终于来了
  7. gg修改器修改内购_【教程】手把手教你修改微信性别为空
  8. 有了这5个免费下载音乐的wangzhan,从此告别各种VIP
  9. windows11关闭安全中心的病毒和威胁防护时,打不开,弹出打开应用需要打开windowsdefender链接问题
  10. 赤兔之死高考满分作文原文及赏析
  11. elasticSearch搜索引擎安装使用,SprignBoot整合Spring Data Elasticsearch,百度云 Elasticsearch安装包
  12. 分享一个强大的网盘搜索平台-猪猪盘
  13. 你知道PDF怎么合并吗?这些技巧快来码住
  14. GitHub 3.6k Satr自监督学习(Self-Supervised Learning)资源你值得拥有!
  15. 开发电商系统用什么开发语言呢
  16. The flowing processes must be stopped before the CUDA Visual studio integration installation can pro
  17. 发射蓝色/绿色/红外金属卤化物CsPbBr钙钛矿量子点发光二极管
  18. TB-RK3399Pro 找不到网络图标,如何连wifi呢?
  19. 在线文件不落地转base64
  20. 如何把图片压缩到20k一下?怎么降低照片大小kb?

热门文章

  1. Git教程学习总结(分享给热爱学习的你,团队的协作离不开你呀)
  2. 将 改为c语言表达式,基于c语言表达式求解课程设计修改.doc
  3. java中报错convension_LambdaConversionException与泛型:JVM错误?
  4. python扩展文件_1. 使用 C 或 C++ 扩展 Python
  5. C语言bcd码减法过程,bcd码的减法运算规则举例.ppt
  6. AJAX跨域问题解决方法(1)——禁止浏览器进行跨域限制
  7. CSS的常用属性(二)
  8. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(44)-工作流设计-设计表单...
  9. 替换Quartus 自带编辑器 (转COM张)
  10. commons-lang的FastDateFormat性能测试