博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
scala中def_def关键字以及Scala中的示例
阅读量:2530 次
发布时间:2019-05-11

本文共 2236 字,大约阅读时间需要 7 分钟。

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 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返回类型返回两个数字的和,作为函数的参数传递。 返回的值使用主类中的打印功能进行打印。

翻译自:

scala中def

转载地址:http://lvtzd.baihongyu.com/

你可能感兴趣的文章
考勤系统之计算工作小时数
查看>>
4.1 分解条件式
查看>>
Equivalent Strings
查看>>
flume handler
查看>>
收藏其他博客园主写的代码,学习加自用。先表示感谢!!!
查看>>
H5 表单标签
查看>>
su 与 su - 区别
查看>>
C语言编程-9_4 字符统计
查看>>
在webconfig中写好连接后,在程序中如何调用?
查看>>
限制用户不能删除SharePoint列表中的条目(项目)
查看>>
【Linux网络编程】使用GDB调试程序
查看>>
feign调用spring clound eureka 注册中心服务
查看>>
ZT:Linux上安装JDK,最准确
查看>>
LimeJS指南3
查看>>
关于C++ const成员的一些细节
查看>>
《代码大全》学习摘要(五)软件构建中的设计(下)
查看>>
C#检测驱动是否安装的问题
查看>>
web-4. 装饰页面的图像
查看>>
微信测试账户
查看>>
Android ListView上拉获取下一页
查看>>