scala中def

Scala def关键字 (Scala def keyword)

The def keyword in Scala is used to declare functions and methods in Scala. Scala being ignorant on the data types does the same with the return type of a function. Declaring and defining a function in Scala does not strictly require a return type. The def keyword usage makes the Scala program more flexible.

Scala中def关键字用于在Scala中声明函数和方法。 对数据类型无知的Scala与函数的返回类型相同。 在Scala中声明和定义函数并不严格要求返回类型。 def关键字的使用使Scala程序更加灵活。

The function or methods that are defined using Scala def keyword get evaluated when they are called. This practice reduces the load on compiler because if a function is not called in some case. It is not evaluated.

使用Scala def关键字定义的函数或方法在被调用时会得到评估。 这种做法减少了编译器的负担,因为如果在某些情况下未调用函数。 不评估。

A function is said to be an anonymous function ( without a name ) if it is declared without using the def keyword and cannot be referenced. So, the functions with def keyword are used when the function call is required. And giving a name to it is important and using def keyword allows it.

如果某个函数未使用def关键字进行声明且无法引用,则称该函数为匿名函数(无名称)。 因此,当需要调用函数时,将使用带有def关键字的函数。 给它起一个名字很重要,并且使用def关键字允许它。

Syntax (declaration):

语法(声明):

    def function_name(arguments ) : returntype;

Definition:

定义:

    def function_name(arguments) : returntype {
//code to be executed...
}

Syntax explanation:

语法说明:

Here, Scala def keyword is used to define the function, the set of arguments of the function are enclosed in the brackets and an optional return type can also be given.

在这里,使用Scala def关键字定义函数,函数的参数集放在方括号中,还可以提供可选的返回类型。

Example code:

示例代码:

object MyClass {def add(x:Int, y:Int) : Int = {var sum = x+y ;
return sum;
}
def main(args: Array[String]) {print("sum of x + y = " + add(25,10));
}
}

Output

输出量

sum of x + y = 35

Code explanation:

代码说明:

The above code prints the sum of two numbers using a function. The function add is used to add two numbers and returns their result. The function used Int return type to return the sum of two numbers passes as arguments of the function. The returned value is printed using the print function in the main class.

上面的代码使用一个函数打印两个数字的和。 函数add用于将两个数字相加并返回其结果。 该函数使用Int返回类型返回两个数字的和,作为函数的参数传递。 返回的值使用主类中的打印功能进行打印。

翻译自: https://www.includehelp.com/scala/def-keyword-with-example-in-scala.aspx

scala中def

scala中def_def关键字以及Scala中的示例相关推荐

  1. java构造方法中this_Java中this关键字在构造方法中的使用

    1. Java中this关键字代表对象本身.用this关键字可以在类的内部调用属性和方法,这样代码的可读性比较高,因为它明确的指出了这个属性或方法的来源. 2. 同时在构造函数中也可以使用this关键 ...

  2. static在php中,php中static关键字在类中的使用

    static关键字用来修饰属性.方法,称这些属性.方法为静态属性.静态方法. static关键字声明一个属性或方法是和类相关的,而不是和类的某个特定的实例相关,因此,这类属性或方法也称为"类 ...

  3. java中的关键字 保留字_java中的标识符、关键字、保留字

    Java中关键字(keyword)和保留字(reservedword) Keyword :Java的关键字对java的编译器有特殊的意义,他们用来表示一种数据类型,或者表示程序的结构等. Reserv ...

  4. sql优化中in关键字_工作中遇到的一个SQL优化问题与解决方案

    我们公司是做汽车服务行业SCRM门店管理系统的,其中一个功能是查出该门店的所有会员与之对应的车辆.有三张表,核心字段如下: 需求:查出某个门店下的所有会员与车辆列表(会员姓名,车辆品牌,车牌号,车标l ...

  5. 超大文件中查找关键字

    一个有10亿条记录的文本文件,已按照关键字排好序存储.请设计算法,可以快速的从文件中查找指字关键字的记录. 因为已经排好序了,可以使用 二分查找方法检索. 又因为文件过于庞大,可以将文件File分成1 ...

  6. Scala深入浅出——从Java到Scala

    本文适合有一定Java基础的,并想系统学习Scala的小伙伴借鉴学习.文章有大量实例,建议自己跑一遍. Scala深入浅出--从Java到Scala Scala 一.介绍 1.什么是Scala? 2. ...

  7. scala语言示例_var关键字与Scala中的示例

    scala语言示例 Scala var关键字 (Scala var keyword) The var Keyword in scala is used to declare variables. As ...

  8. Scala中lazy关键字的使用和理解

    Scala中lazy关键字的使用和理解 转载声明: 本文转自 Scala中lazy关键字的使用和理解 作者:br0x 转载仅为方便学习查看,一切权利属于原作者,如果带来不便请联系我删除. Scala中 ...

  9. 【scala】Scala中lazy关键字的使用和理解

    Scala中使用关键字lazy来定义惰性变量,实现延迟加载(懒加载). 惰性变量只能是不可变变量,并且只有在调用惰性变量时,才会去实例化这个变量. 在Java中,要实现延迟加载(懒加载),需要自己手动 ...

最新文章

  1. linux 脚本 变量为空,Shell脚本中判断输入变量或者参数是否为空的方法
  2. C语言高级编程:如何确定一个变量是有符号还是无符号数
  3. linux临时启动进程命令,Linux常用命令(一)服务控制及优化启动过程
  4. 读书,上学,上名校!!!!!
  5. C# 使用Quartz简单实例以及备忘
  6. 当网络安全遇上大数据分析(1)
  7. 【原创】有关Buffer使用,让你的日志类库解决IO高并发写
  8. 关于事业发展,雷林鹏教你如何找到方向?
  9. 网络布线指南:光纤基础知识大扫盲
  10. 守卫数据中心命门的胡桃夹子-特权账号管理平台
  11. android nmea 工具,android – 如何从nmea句子信息计算以米为单位的gps准确度
  12. 转载:optparse模块OptionParser学习
  13. 【读书笔记 1】《读大江大河 有感》
  14. 阿里云数据盘挂载完整过程
  15. 《我的世界》游戏攻略:如何一建生成房屋指令?
  16. 线性电阻电路方程的建立方法
  17. 《机器学习实战》学习笔记(八):预测数值型数据 - 回归
  18. DCC尺寸链计算与公差仿真分析软件 软件应用案例以及授权文件
  19. 快递员能开你家锁了,问你怕不怕?
  20. 乱七八糟记一下乱七八糟的碎片化知识

热门文章

  1. 插入排序之表插入排序
  2. 媒体服务器协议,媒体服务器介绍(mediactrl架构)
  3. linux内核中cent文件夹,Centos 中如何快速定制二进制的内核 RPM 包
  4. python能和wincc一起用_搬家第二天-41.Wincc V7.3 利用Inputbox和Msgbox做权限管理和二次确认设计...
  5. centos odbc mysql_Centos下安装并配置ODBC连接MySQL 【转】
  6. 树莓派摄像头_Arducam 8MP重磅来袭,为树莓派4B构建完全同步的双摄像头方案~
  7. Juypter 代码自动补全
  8. 百度MIP移动页面加速——不只是CDN
  9. apache ab压力测试报错
  10. 为回馈广大学员,智捷课堂买关老师Cocos2d-x课程送Cocos2d-x图书