scala 方法重載_Scala中的方法重載

scala 方法重載

Scala方法重載 (Scala method overloading)

Method overloading is a method that is redefined in different ways under the same name. Method overloading is one of the methods used to implement polymorphism in Scala.

方法重載是一種使用相同名稱以不同方式重新定義的方法。 方法重載是用于在Scala中實現多態的方法之一。

Implementation of method overloading in Scala

Scala中方法重載的實現

To create an overloaded method in Scala, define multiple methods with the same name and different parameter list and return type. This means defining multiple functions one with one parameter of type ‘A’ other with two parameters, etc.

在Scala中創建重載方法 ,請定義多個具有相同名稱,不同參數列表和返回類型的方法。 這意味著定義多個功能,一個具有一個“ A”類型的參數,另一個具有兩個參數,等等。

Syntax:

句法:

    //Method 1 : 
def fun (a : data_type ) { 
// code to be executed
}
//Method 2 :
def fun(a : data_type , b : data_type ){
// code to be executed
}

Both these methods contribute to method overloading in Scala. Now, to create an overloaded method can invert either of the following things in the function that lead to overloading the method:

這兩種方法都會導致Scala中的方法重載。 現在,要創建一個重載的方法,可以在函數中反轉以下任何事情,從而導致該方法的重載

  1. Methods with different parameter lists

    具有不同參數列表的方法

  2. Methods with different data types and order

    具有不同數據類型和順序的方法

Program to show implementation of method overloading in Scala | print area of different figures using method overloading

程序展示Scala中方法重載的實現 使用方法重載來打印不同圖形的區域

object MyClass {
def peri(x:Int, y:Int){
println("The perimeter of rectangle is "+ (x+y))   
}
def peri(a:Int , b:Int ,c:Int){
println("The perimeter of rectangle is "+ (a+b+c))   
}
def peri(r:Int){
println("The perimeter of rectangle is "+ (2*(3.14)*r))   
}
def main(args: Array[String]) {        
println("Program to print perimeter of different figures using method overloading: ")
println("Perimeter of rectangle: ")
peri(12 , 345)
println("Perimeter of triangle: ")
peri(3, 5, 8)
println("Perimeter of circle:")
peri(4)
}
}

Output

輸出量

Program to print perimeter of different figures using method overloading: 
Perimeter of rectangle: 
The perimeter of rectangle is 357
Perimeter of triangle: 
The perimeter of rectangle is 16
Perimeter of circle:
The perimeter of rectangle is 25.12

Explanation:

說明:

The above code is used to print demonstrate method overloading concept. This example is to print the perimeter of different figures using object overloading. The function peri() is overloaded and each of the different figure areas will have a different overloaded method that will be able to calculate the given area of the specific figure. The code prints the calculated value for the figure.

上面的代碼用于打印演示方法重載的概念。 本示例將使用對象重載來打印不同圖形的周長。 函數peri()已重載,每個不同的圖形區域將具有不同的重載方法,該方法將能夠計算特定圖形的給定區域。 該代碼將打印圖形的計算值。

Program to show implementation of method overloading in Scala | print data-type of the parameter using method overloading

程序展示Scala中方法重載的實現 使用方法重載打印參數的數據類型

object MyClass {
def datatype(x:Int){
println("The parameter is of Integer datatype")    
}
def datatype(x:Float){
println("The parameter is of Float data type")    
} 
def datatype(x:Char){
println("The parameter is of Character data type")    
}
def datatype(x: Boolean){
println("The parameter is of Boolean data type")    
}
def main(args: Array[String]) {
println("Program to print data type using method overloading: ")
datatype(4)
datatype(4.0f)
datatype('f')
datatype(true)
}
}

Output

輸出量

Program to print data type using method overloading: 
The parameter is of Integer datatype
The parameter is of Float data type
The parameter is of Character data type
The parameter is of Boolean data type

Explanation:

說明:

The above code is used to print the data type of the variable. The method datatype() is overloaded to print the data type based on the input variable. The method can check for Int, Float, Char, Bool data type and will check for the data type, and prints the data type accordingly.

上面的代碼用于打印變量的數據類型。 方法datatype()重載以根據輸入變量輸出數據類型。 該方法可以檢查Int , Float , Char , Bool數據類型,并將檢查該數據類型,并相應地打印該數據類型。

翻譯自: https://www.includehelp.com/scala/method-overloading-in-scala.aspx

scala 方法重載

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377463.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377463.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377463.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

C#網頁自動登錄和提交POST信息的多種方法 新人學習中

網頁自動登錄和提交POST信息的核心就是分析網頁的源代碼(HTML),在C#中,可以用來提取網頁HTML的組件比較多,常用的用WebBrowser、WebClient、HttpWebRequest這三個。 以下就分別用這三種方法來實現:1、WebBr…

四、采集和制作數據集

一、采集數據 安裝labelme:pip install labelme 打開labelme:labelme 將收集好的照片(320320,png格式)存放到一個文件夾中,例如我的是F:\test,再此文件夾下再創建個文件夾label用于存放標簽文件 使用labelme打開數據…

MTFBWU的完整形式是什么?

MTFBWU:愿力量與您同在 (MTFBWU: May The Force Be With You) MTFBWU is an abbreviation of “May The Force Be With You". MTFBWU是“愿力量與你同在”的縮寫 。 It is an expression, which is commonly used in messaging or chatting on social media n…

VMware14.0 安裝 CentOS7.2

大致流程 對于VMware14.0安裝包用百度網盤下載即可。 鏈接:https://pan.baidu.com/s/1DEGa47EbI1Fup_MTXhv0xg 提取碼:izo6 華為云CentOS7 下載劃線的。其他步驟與大致流程里一樣。 最后輸入root 以及配置的密碼即可:密碼輸入時是沒有任何顯…

基于visual Studio2013解決C語言競賽題之1049抓牌排序

題目解決代碼及點評/* 功能:插入排序。許多玩牌的人是以這樣的方式來對他們手中的牌進行排序的:設手中原有3張牌已排好序,抓1張新牌,若這張新牌的次序在…

學習Lucene筆記一:創建索引

public class HelloLucene {/*** 建立索引* param args*/public void index(){IndexWriter writer null; try {//1.創建Directory,// Directory directory new RAMDirectory();//索引是建立在內存中的Directory directory FSDirectory.open(new File("D:/Lucene/ind…

【C++進階】C++創建文件/屏幕輸出流類(將信息同時輸出到文件和屏幕)

在軟件的調試技術中,很重要的一個技術是將軟件運行過程中的一些信息寫入到“日志文件”中。但是同時還要將信息顯示到屏幕上,以方便程序員實時查看這些信息。 最簡單的一種辦法是這樣的: std::ofstream output("debug.log", ios::…

五、加載數據集

之前寫過加載數據集的一些小筆記,這里詳細內容就不再敘述了 詳細學習可以參考該博文二、PyTorch加載數據 一、分析 因為U-net網絡架構是輸入1通道,大小為(572,572)的灰度圖,圖片大小無所謂,我的思路是將三通道的圖像使用OpenCV進…

CDMA的完整形式是什么?

CDMA:碼分多址 (CDMA: Code Division Multiple Access) CDMA is an abbreviation of Code Division Multiple Access. Code Division Multiple Access is a digital cellular technology and displays a network of multiple accesses. The various radio communica…

BCD碼與十進制的相互轉換

BCD碼是用每四位代替一位十進制數(0 到 9 的某一位數) 例如:0x25 就代表25 十六進制的每位轉換成二進制代表四個位。 下面是bcd轉char short int long c語言程序 //************************************************************…

DSP關于存儲器讀寫、IO讀寫時序圖的注意點

這里的存儲器圖不涉及插入等待周期。 IO設備的圖可以自行減去插入等待周期,然后觀察。 存儲器讀讀寫 存儲器寫寫讀 I/O設備讀寫操作

折騰430 launchpad

launchpad到手也已經很長時間了,團購了一個g2的,一個鐵電的,現在馬上又要來一個g2的,感覺手上的東西太多了,急需消化一下,首先呢還是先把430搞定吧。 ---------------------------------------------------…

oem模式是什么_OEM的完整形式是什么?

oem模式是什么OEM:原始設備制造商 (OEM: Original Equipment Manufacturer) OEM is an abbreviation of "Original Equipment Manufacturer". Its meaning has changed over time. In former times, it alluded to a corporation that manufactures produ…

媽了個巴卡

配置文件修改: 一、打開PC端微信,打開咩了個咩小程序,點進入第一關,之后再關掉小程序 二、PC端微信設置里面,找到管理文件,打開文件夾 三、Applet下按修改日期找到a9結尾的文件 四、接著進入\usr\gamecac…

java中Iterator的小程序

import java.util.Collection; import java.util.HashSet; import java.util.Iterator;public class TestIterator {public static void main(String[] args){Collection booksnew HashSet();books.add("java講義");books.add("java的Ajax寶典");books.add…

【C++進階】利用重載二元運算符改進平面向量類Vec2D

先前回顧 在【C進階】 遵循TDD原則,實現平面向量類(Vec2D)中我們初步實現了Vec2D內容,現在做出一定的改進: 實現Vec2D的一半二元算數運算符重載 1、 - (兩個Vec2D對象運算以及1個Vec2D對象與一個double數運算) 2、*(點乘和數乘) 同時將之前…

在SQL中使用DEFAULT約束

DEFAULT constraint is used to insert default value into a column on a table and if no any value is stored in any place of a column then default value will be added into it. DEFAULT約束用于將默認值插入到表的列中,如果列的任何位置均未存儲任何值&…

(ios7) 解決代碼布局View, ios7 中 subView 高度增加StatusBar20dp的問題,保證Ios6,ios7代碼一致...

在ios7 布局中,Status Bar 和 ToolBar ,NavigateBar 等都包含在ViewControl的主View中。 這樣原來ios6 的View布局 整體向上移動了20dp,下面是保證ios6,ios7代碼一致的解決方案 1 第一步 在項目的Info.plist 文件中 添加一行屬性配置 View co…

簡單的群體測試方案C++代碼(Group testing against Covid-19)

原理參考鏈接 https://www.econstor.eu/handle/10419/221811 http://www.magigen.com/h-nd-348.html 文章原理回顧 文章比較了兩種估計人群中病毒流行率的方法: 1、個體測試,即對12000人的樣本進行病毒測試,并采用標準二項測試得出95%的置…

使用WinDbg和VMware調試NDIS中間層驅動程序 (轉)

使用WinDbg和VMware調試NDIS中間層驅動程序 我這里將一步一步的介紹,是從新手的角度來講的,所以對高手來說,可能有些啰嗦。如果你看完這篇文章還不知道如何設置,那么原因可能有兩個:1. 我沒講好;2. 你需要稍…