scala特质_Scala的特质

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特质

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/377041.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

优化PHP代码的40条建议(转)

优化PHP代码的40条建议 40 Tips for optimizing your php Code 原文地址&#xff1a;http://reinholdweber.com/?p3 英文版权归Reinhold Weber所有&#xff0c;中译文作者yangyang&#xff08;aka davidkoree&#xff09;。双语版可用于非商业传播&#xff0c;但须注明英文版作…

Iptables入门教程

转自&#xff1a;http://drops.wooyun.org/tips/1424 linux的包过滤功能&#xff0c;即linux防火墙&#xff0c;它由netfilter 和 iptables 两个组件组成。 netfilter 组件也称为内核空间&#xff0c;是内核的一部分&#xff0c;由一些信息包过滤表组成&#xff0c;这些表包含内…

《线程管理:传递参数、确定线程数量、线程标识》

参考《c Concurrency In Action 》第二章做的笔记 目录传递参数量产线程线程标识传递参数 thread构造函数的附加参数会拷贝至新线程的内存空间中&#xff0c;即使函数中的采纳数是引用类型&#xff0c;拷贝操作也会执行。如果我们期待传入一个引用&#xff0c;必须使用std::re…

手把手玩转win8开发系列课程(14)

这节的议程就是——添加appbar appbar是出现在哪儿了&#xff0c;出现在屏幕的底部。他能使用户能用手势或者使用鼠标操作程序。metro UI 重点是在主要的控件使用许多控件&#xff0c;使其用户使用win8电脑更加的方便。而appBar使其用户体验更好。在这节中&#xff0c;我将告诉…

No identities are available for signing 的解决办法

今天重新上传做好的app提交到app store&#xff0c;结果就出现标题上的错误。“No identities are available for signing”。 以后碰到这样的问题按照下面几个步骤来做&#xff1a; 进入Distribution -----下载发布证书 -----双击安装-----重启Xcode就能上传了 其他细节 如果再…

半连接反连接

半连接&反连接 1. 半连接 半连接返回左表中与右表至少匹配一次的数据行&#xff0c;通常体现为 EXISTS 或者 IN 子查询。左表驱动右表。只返回左表的数据&#xff0c;右表作为筛选条件。 可以用 EXISTS、 IN 或者 ANY 举例&#xff1a;表t1和表t2做半连接&#xff0c;t…

匿名方法和Lambda表达式

出于MVVM学习的需要&#xff0c;复习下匿名方法和Lambda表达式&#xff0c;因为之前用的也比较少&#xff0c;所以用的也不是很熟练&#xff0c;Baidu下相关的知识&#xff0c;写了这个Demo&#xff0c;目标是用简单的方法展示这个怎么用。 这里偏重的和LINQ中的Lambda表达式 …

烂橘子

Problem Statement: 问题陈述&#xff1a; Given a matrix of dimension r*c where each cell in the matrix can have values 0, 1 or 2 which has the following meaning: 给定尺寸r * C的矩阵&#xff0c;其中矩阵中的每个单元可以具有其具有以下含义的值0&#xff0c;1或2…

android junit 测试程序

http://blog.csdn.net/to_cm/article/details/5704783 Assert.assertEquals(2, t); 断言转载于:https://www.cnblogs.com/wjw334/p/3714120.html

MySQL 8.0.22执行器源码分析HashJoin —— BuildHashTable函数细节步骤

BuildHashTable函数细节步骤 该函数位置处于hash_join_iterator.cc 403 ~ 560行 step1&#xff1a;如果被驱动表迭代器没有更多的行数&#xff0c;更新m_state为EOR&#xff0c;然后返回false&#xff0c;表明创建hash表失败 if (!m_build_iterator_has_more_rows) {m_state…

《那些年啊,那些事——一个程序员的奋斗史》——125

距离离职交接的一个月时间还剩几天&#xff0c;本来应该是平淡无事的&#xff0c;却没想到最后还是波澜四起。昨天下班前&#xff0c;公司突然停了电。这本是件普通得不能再普通的事情&#xff0c;可没想到过了一会来电了&#xff0c;或许是波峰电压太大&#xff0c;或许是稳压…

python中的元类_Python中的元类

python中的元类Python元类 (Python metaclass) A metaclass is the class of a class. A class defines how an instance of a class i.e.; an object behaves whilst a metaclass defines how a class behaves. A class is an instance of a metaclass. 元类是类的类。 一个类…

MySQL 8.0.22执行器源码分析HashJoin —— 一些初始化函数的细节步骤

目录InitRowBuffer&#xff08;101行~126行&#xff09;InitProbeIterator&#xff08;142行~153行&#xff09;*HashJoinIterator* 的Init&#xff08;155行~240行&#xff09;InitializeChunkFiles&#xff08;364行~401行&#xff09;InitWritingToProbeRowSavingFile&#…

c语言的宏定义学习笔记

宏定义 在预处理之前&#xff0c;c预处理器会对代码进行翻译&#xff0c;譬如用blank替换注释&#xff0c;去掉多余的空格&#xff0c;删除末尾的\来拼接行等。 例如&#xff1a; int /*注释*/ x; 会被翻译成 int x; printf("this is a s\ entence."); 会被翻译成 pr…

摄氏温度转换华氏温度_什么是摄氏温度?

摄氏温度转换华氏温度摄氏温度 (Celsius) Celsius is a temperature measuring scale which as a SI unit derived from the seven base units stated and described by the International System of Units (SI). 摄氏温度是一种温度测量刻度&#xff0c;它是由国际单位制(SI)所…

别人的算法学习之路

http://www.cnblogs.com/figure9/p/3708351.html 我的算法学习之路 关于 严格来说&#xff0c;本文题目应该是我的数据结构和算法学习之路&#xff0c;但这个写法实在太绕口——况且CS中的算法往往暗指数据结构和算法&#xff08;例如算法导论指的实际上是数据结构和算法导论&a…

git config命令使用第二篇——section操作,多个key值操作,使用正则

接上一篇&#xff0c;git config命令使用第一篇——介绍&#xff0c;基本操作&#xff0c;增删改查:http://blog.csdn.net/hutaoer06051/article/details/8275069 1. 删除一个section 命令参数 --remove-section 格式&#xff1a;git config [--local|--global|--system] --rem…

MySQL面试准备——64页pdf

本笔记为以前整理的零碎的关于Mysql的知识点&#xff0c;有深入源码的也有浅层的八股。已经被我整理成了一个pdf。 实习岗位正好也是和数据库内核有关的&#xff0c;之后应该还会更新。做个整理&#xff0c;方便秋招的时候快速回顾吧。 链接&#xff1a;链接 提取码&#xff1a…

python点图_Python | 点图

python点图The dot plot is a type of data representation in which each data-point in the figure is represented as a dot. Dot plot underlies discrete functions unlike a continuous function in a line plot. Each value could be correlated but cannot be connecte…

SAP-MM:发票、贷方凭证、事后借记、后续贷记

发票和事后借记 相同点&#xff1a;增加对供应商的应付款 不同点&#xff1a;针对同一订单收货&#xff0c;发票要先于事后借记&#xff08;事后借记是对供应商后期发票金额的补充&#xff09;&#xff1b;发票和金额、订单数量有关系&#xff0c;而事后借记只是订单金额调整的…