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 方法重载

scala 方法重载_Scala中的方法重载相关推荐

  1. scala 方法调用_Scala中的方法调用

    scala 方法调用 Scala方法调用 (Scala Method Invocation) Method invocation is the legal and correct technique ...

  2. java 两个运算符重载_Java中的操作符重载

    0.Java操作符重载 Java中我们可以执行如下语句: String fullName = "hongliang "+"cao"; System.out.pr ...

  3. java 方法的重载_Java中的方法和方法重载

    今天我们来说说Java中的方法和方法重载以及需要注意的一些地方; 方法: Java的方法类似与其他语言的函数,是一段用来完成特定功能的代码片段, 声明格式: [修饰符1  修饰符2 ....]  ,返 ...

  4. 【Groovy】集合遍历 ( 操作符重载 | 集合中的 “ + “ 操作符重载 | 集合中的 “ - “ 操作符重载 | 代码示例 )

    文章目录 一.集合中的 " + " 操作符重载 二.集合中的 " - " 操作符重载 三.完整代码示例 一.集合中的 " + " 操作符重载 ...

  5. java 方法大全_java中的方法大全

    方法: 一.什么是方法? 就是有名字的代码段.适用于函数. 方法的定义只管某个功能的实现,只保证功能的可用. 二.方法的目的是什么? 为了代码的重用.(相同的类型的操作,不用重复的写代码) 三.方法的 ...

  6. java构造函数的重载_Java中的构造函数重载 - Break易站

    Java 构造函数 Java中的构造函数重载 除了重载方法外,我们还可以在java中重载构造函数.基于新执行时指定的参数调用重载的构造函数. 我们什么时候需要构造函数重载? 有时候需要用不同的方式初始 ...

  7. java 非法重载_JAVA中重写和重载区别

    重写和重载区别 重写方法的规则如下: 参数列表:必须与被重写方法的参数列表完全匹配. 返回类型:必须与超类中被重写的方法中声明的返回类型或子类型完全相同 访问级别:一定不能比被重写方法强,可以比被重写 ...

  8. java中函数的重载_Java中函数的重载

    函数的重载 1.同一个类 2.同名函数 3.参数个数不同或者参数类型不同 4.java是严谨性语言,如果函数出现的调用的不确定性,会编译失败. public static int add(int a, ...

  9. scala 字段覆盖_Scala中的字段覆盖

    scala 字段覆盖 Scala字段覆盖 (Scala field overriding) Overriding is the concept in which the child class is ...

最新文章

  1. Ubuntu使用wget下载zabbix的扩展源
  2. Techparty-广州Javascript技术专场(学习分享)
  3. tomcat的热部署
  4. Github 2020 年度报告:你以为新冠击溃了开发者?不!他们创造了更多代码...
  5. ssh连接出现:WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
  6. 下运行maven命令_如何在批处理模式下运行 top 命令
  7. scrapy-redis爬虫如何发送POST请求
  8. Source code manager common
  9. 安全公司本意告警用户,不料先遭攻击并泄露超50亿个人数据
  10. lightoj 1016
  11. N1网络命令-ping
  12. mysql架构 三级主从同步_MySQL 主从同步架构中你不知道的“坑”(完结篇)
  13. 从GPU诞生说起:AMD统一渲染架构回顾及展望(转)
  14. fpga的jtag接口扫不到器件_JTAG接口的定义及常见问题
  15. ddm模型公式_cfa讲义-估值中的折现方法-DDM模型(2)
  16. 15个素材下载网站,从此不做「伸手党」!
  17. 安装macOS时遇到Unable to unmount volume for repair异常导致无法完成安装的解决办法
  18. 2021年4月11日-粤嵌智能小车兴趣课笔记(3)
  19. 设计一个xml格式的文件
  20. Reasoning-RCNN 论文笔记

热门文章

  1. C语言 删除文件 M,最全的C盘可删除文件清单
  2. mongodb 监控权限_运维监控产品分析篇
  3. java 文件缓冲区_Java开发笔记(八十六)通过缓冲区读写文件
  4. vscode修改python终端_panda3d是python的一个高级的3D 渲染和游戏开发框架
  5. swal ajax,Sweetalert详细介绍
  6. c语言错误重定义,C语言的重定义错误求解
  7. 字符数组和字符串的小细节
  8. element实现动态路由+面包屑
  9. 在java web工程中jsp页面中使用kindeditor
  10. TJOI2018Party