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.pswp.cn/news/377041.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377041.shtml
英文地址,請注明出處:http://en.pswp.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;而事后借記只是訂單金額調整的…