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

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/540992.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

前端必备的 web 安全知识手记

前言 安全这种东西就是不发生则已,一发生则惊人。作为前端,平时对这方面的知识没啥研究,最近了解了下,特此沉淀。文章内容包括以下几个典型的 web 安全知识点:XSS、CSRF、点击劫持、SQL 注入和上传问题等(…

php脚本开头注释_PHP文件注释标记及规范小结

PHP文件注释标记及规范小结发布时间:2016-06-17 来源: 点击:次PHP 注释标记access使用范围:class,function,var,define,module该标记用于指明关键字的存取权限:private、public或protecedauthor指明作者copyright使用范围&#xf…

Salesforce宣布5.82亿美元收购文件编辑公司Quip

北京时间8月2日消息,据路透社报道,云软件开发商Salesforce.com周一宣布,已同意以大约5.82亿美元收购文件编辑创业公司Quip。 Salesforce此前已经对Quip进行了投资。Quip开发的文字处理平台供企业员工用于在移动设备、可穿戴设备以及台式机上编…

Java中的main()方法是强制性的吗?

The question is that "Is main() method is compulsory in Java?" 问题是“ main()方法在Java中是强制性的吗?” Yes, we can write a java program without main() method but there is a condition if and only if java JDK version till JDK 5. 是的…

php date( w ),PHP Date()函数详解

页面的最上方加上:date_default_timezone_set(PRC); /*把时间调到北京时间,php5默认为格林威治标准时间*/date ()a: "am"或是"pm"A: "AM"或是"PM"d: 几日,两位数字,若不足则补零&#xff1b…

10.6-全栈Java笔记:常见流详解(四)

上节我们讲到「Java中常用流:数据流和对象流」,本节我们学习文件字符流和文件缓冲流~文件字符流前面介绍的文件字节流可以处理所有的文件,但是字节流不能很好的处理Unicode字符,经常会出现“乱码”现象。所以,我们处理…

python 示例_带有示例的Python File open()方法

python 示例文件open()方法 (File open() Method) open() method is an inbuilt method in Python, it is used to create, open or append a file. open()方法是Python中的内置方法,用于创建,打开或附加文件。 Syntax: 句法: file_object …

php属于脚本,php是脚本语言吗

PHP即“超文本预处理器”,是一种通用开源脚本语言。PHP是在服务器端执行的脚本语言,与C语言类似,是常用的网站编程语言。PHP独特的语法混合了C、Java、Perl以及 PHP 自创的语法。利于学习,使用广泛,主要适用于Web开发领…

NetMarketShare:本月桌面浏览器市场份额几乎没有变化

NetMarketShare之前关于台式机浏览器市场份额的报告表示,Google Chrome市场份额正在快速上升,而Edge浏览器市场份额正在以蜗牛的速度前进。而该公司的最新统计数据显示,几乎所有浏览器的市场份额或多或少保持不变。 NetMarketShare的最新统计…

treeset java_Java TreeSet add()方法与示例

treeset javaTreeSet类的add()方法 (TreeSet Class add() method) add() method is available in java.util package. add()方法在java.util包中可用。 add() method is used to add the given object(ob) to this TreeSet when it does not already exist otherwise it ignore…

php fpm www.conf,PHP7中php.ini、php-fpm和www.conf 配置

PHP7中php.ini、php-fpm和www.conf 配置php.ini是php运行核心配置文件,下面是一些常用配置extension_dir""● 设置PHP的扩展库路径expose_php Off● 避免PHP信息暴露在http头中display_errors Off● 避免暴露php调用mysql的错误信息log_errors On● 在关闭display…

服务器电流源泉ups电源的三大形式

还记得此前12306官网瘫痪,回家心切急于购票的我们只能感到无比心累。双十一前夕,守在购物车边准备疯狂购物的剁手党们,遇到一直呈现加载状态的页面,不得不感叹想要做马云背后的那个人也要大费周折。作为一个资深网民,不…

timer purge_Java Timer purge()方法与示例

timer purge计时器类purge()方法 (Timer Class purge() method) purge() method is available in java.util package. purge()方法在java.util包中可用。 purge() method is used to remove all canceled tasks from the task queue of this Timer. purge()方法用于从此Timer的…

大话Linux内核中锁机制之原子操作、自旋锁【转】

转自:http://blog.sina.com.cn/s/blog_6d7fa49b01014q7p.html 多人会问这样的问题,Linux内核中提供了各式各样的同步锁机制到底有何作用?追根到底其实是由于操作系统中存在多进程对共享资源的并发访问,从而引起了进程间的竞态。这…

hashmap put方法_Java HashMap put()方法与示例

hashmap put方法HashMap类的put()方法 (HashMap Class put() method) put() method is available in java.util package. put()方法在java.util包中可用。 put() method is used to link the given value element with the given key element in this HashMap. put()方法用于在…

java中jdom,java – JDOM中的命名空间(默认)

我正在尝试使用最新的JDOM包生成XML文档.我遇到了根元素和命名空间的问题.我需要生成这个根元素:xmlns"http://www.energystar.gov/manageBldgs/req"xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation"http://www.…

java enummap_Java EnumMap get()方法与示例

java enummapEnumMap类的get()方法 (EnumMap Class get() method) get() method is available in java.util package. get()方法在java.util包中可用。 get() method is used to get the value mapped with the given key element (key_ele) otherwise it returns null when no…

java后台json传递,后台json传递

json除了可以用于前台传递,还可用于后台之间传递。它可以传递List,Map,Bean等类型的数据。例如:User u1new User();u1.setUsername("zy");u1.setPassword("123");User u2new User();u2.setUsername("msl");u2.setPassword…

专家呼吁建安全漏洞信息共享机制并强化管控

近日,由中国网络空间安全协会主办,中国网络空间安全协会网络空间安全法律与公共政策专业委员会、北京邮电大学互联网治理与法律研究中心、公安部第三研究所网络安全法律研究中心、西安交通大学信息安全法律研究中心承办的《网络安全法(草案二…

java的equals方法_Java Date equals()方法与示例

java的equals方法日期类equals()方法 (Date Class equals() method) equals() method is available in java.util package. equals()方法在java.util包中可用。 equals() method is used to check whether this date and the given object (o) are equals or not. equals()方法…