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.mzph.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. 你需要稍…