博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在Scala中评估val,var,lazy val和def构造时
阅读量:2533 次
发布时间:2019-05-11

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

发表简短目录 (Post Brief TOC)

  • Introduction

    介绍
  • Scala ‘val’ usage

    Scala“ val”用法
  • How Scala ‘val’ is Evaluated?

    如何评估Scala的“ val”?
  • Scala ‘var’ usage

    Scala“ var”用法
  • How Scala ‘var’ is Evaluated?

    如何评估Scala'var'?
  • Scala ‘def’ usage

    Scala'def'用法
  • How Scala ‘def’ is Evaluated?

    Scala的“ def”如何评估?
  • How Scala ‘lazy val’ is Evaluated?

    如何评估Scala的“懒惰值”?
  • var Vs lazy val

    var Vs lazy val
  • Scala val,var,lazy val and def Constructs In-Brief

    Scala val,var,lazy val和def构造In-Brief

介绍 (Introduction)

In this post, we are going to discuss about some basic concepts of Scala Programming Language. Even though, it covers very basic concepts but they are very important to know.

在本文中,我们将讨论Scala编程语言的一些基本概念。 即使它涵盖了非常基本的概念,但了解它们也非常重要。

We can answer some questions like What is val, var and def? What are the differences between them? When they are evaluated? What are the differences between val and lazy val?. These things are required to start working on Scala-Based Projects

我们可以回答一些问题,例如什么是val,var和def? 它们之间有什么区别? 什么时候进行评估? val和lazy val有什么区别? 开始基于Scala的项目时需要这些东西

Scala“ val”用法 (Scala ‘val’ usage)

In Scala, ‘val’ modifier is used to define constants. val means constant or immutable that means we cannot change it’s value once its created.

在Scala中,“ val”修饰符用于定义常量。 val表示常量或不可变,表示一旦创建它便无法更改其值。

Example:-

例:-

scala> val name:String = "Scala"name: String = Scalascala> nameres2: String = Scalascala> name = "Java":11: error: reassignment to val       name = "Java"            ^

Here, we can observe that reassignment to val is prohibited. That means val is used to define Immutable data.

在这里,我们可以看到禁止重新分配给val。 这意味着val用于定义不可变数据。

如何评估Scala的“ val”? (How Scala ‘val’ is Evaluated?)

In Scala, ‘val’ is used to define constants. It evaluates only once. They are evaluated at the time of definition only. Once its evaluated, it reuses same value for all references of it.

在Scala中,“ val”用于定义常量。 它只评估一次。 仅在定义时评估它们。 一旦对其求值,它将为所有引用重复使用相同的值。

It does NOT evaluate every-time we access it.

它不会在我们每次访问它时进行评估。

We will explore this rule by using one simple example as shown below:

我们将通过一个简单的示例来探索此规则,如下所示:

Example:-

例:-

object ValApp extends App{  val number = { println("Constant number is initialized."); 99 }  println("Before Accessing 'number' constant:")  println(number + 1)  println(number + 1)  println(number + 1)}

Output:-

Here I’m using all REPL commands for simplicity purpose only. We can test them using any Scala IDEs like Eclipse Scala IDE or IntelliJ IDE (My favourite IDE).

输出:-

在这里,我仅出于简化目的使用所有REPL命令。 我们可以使用任何Scala IDE(例如Eclipse Scala IDE或IntelliJ IDE(我最喜欢的IDE))对其进行测试。

F:\>scalac ValApp.scalaF:\>scala ValAppConstant number is initialized.Before Accessing 'number' constant:100100100

Here we can observe that “number” Constant is evaluated only once at the time of definition that’s why println output is printed before accessing the variable. Even though we have accessed number Constant three times, it’s initialized only once and printed “Constant number is initialized.” text only once.

在这里,我们可以观察到“数字”常量在定义时仅被评估一次,这就是为什么在访问变量之前先打印println输出的原因。 即使我们已经访问过3次常数,它也只能初始化一次并显示“常数已初始化”。 仅发送一次文字。

Scala“ var”用法 (Scala ‘var’ usage)

In Scala, ‘var’ modifier is used to define variables. var means Variable or Mutable that means we can change it’s value once its created.

在Scala中,“ var”修饰符用于定义变量。 var表示Variable或Mutable,表示一旦创建它就可以更改其值。

Example:-

例:-

scala> var name:String = "Scala"name: String = Scalascala> nameres0: String = Scalascala> name = "Java"name: String = Javascala> nameres1: String = Java

Here, we can observe that reassignment to var is allowed. As var is used to define Mutable data, we can change its value once its created.

在这里,我们可以看到允许重新分配给var。 由于var用于定义Mutable数据,因此我们可以在创建变量后更改其值。

如何评估Scala'var'? (How Scala ‘var’ is Evaluated?)

var is evaluated at the time of definition

It is evaluated only once

在定义时评估var

仅评估一次

Example:-

例:-

object VarApp extends App{  val number = { println("Variable number is initialized."); 99 }  println("Before Accessing 'number' variable:")  println(number + 1)  println(number + 1)  println(number + 1)}

Output:-

输出:-

F:\>scalac VarApp.scalaF:\>scala VarAppVariable number is initialized.Before Accessing 'number' variable:100100100

Scala'def'用法 (Scala ‘def’ usage)

In Scala, def is used to define functions or methods. A Method or Function may or may not have arguments and may or may not have return type.

在Scala中,def用于定义函数或方法。 方法或函数可能具有或不具有参数,并且可能具有或不具有返回类型。

Example:-

例:-

scala> def add(num1:Int, num2:Int): Int = num1 + num2add: (num1: Int, num2: Int)Intscala> add(11,22)res2: Int = 33

As shown above, Method or Function is evaluated only we make a call to it.

如上所示,仅在调用方法或函数时才对其进行评估。

Scala的“ def”如何评估? (How Scala ‘def’ is Evaluated?)

In Scala, def is evaluated lazily. It is not evaluated at the time of definition. It is evaluated whenever we make a call to it. It is evaluated every-time we make a call to it.

在Scala中,def的计算是延迟的。 在定义时不进行评估。 每当我们调用它时,都会对其进行评估。 每次调用它时都会对其进行评估。

Example:-

例:-

object DefApp extends App{  def tax = {    println("Function execution started.")    1100  }  println(tax)  println(tax)  println(tax)}

Output:-

输出:-

Before making a cll to Function:Function execution started.1100Function execution started.1100Function execution started.1100

As shown above output, method or function is evaluated only when we make a call to it. Otherwise, it is not evaluated.

如上面的输出所示,仅在调用方法或函数时才对其进行评估。 否则,将不进行评估。

如何评估Scala的“懒惰值”? (How Scala ‘lazy val’ is Evaluated?)

As we know, val is used to define constants or Immutable Data. In the same way, we use “lazy val” to define Immutable data.

众所周知,val用于定义常量或不可变数据。 同样,我们使用“惰性值”定义不可变数据。

However, lazy val is evaluated lazily and only once. It is evaluated only once when we use it for first time.

It is not evaluated at the time of definition. It is not evaluated every-time we access it.

但是,懒惰val只能被懒惰地评估一次。 当我们第一次使用它时,它只会被评估一次。

在定义时不进行评估。 我们不会在每次访问它时对其进行评估。

Example:-

例:-

object LazyValApp extends App{  lazy val number = { println("Constant number is initialized."); 99 }  println("Before Accessing 'number' constant:")  println(number + 1)  println(number + 1)  println(number + 1) }

Output:-

输出:-

Before Accessing 'number' constant:Constant number is initialized.100100100

Here I’m using same example of ValApp, but changed from “val” to “lazy val” that’s it.

在这里,我使用的是ValApp的相同示例,但仅从“ val”更改为“ lazy val”。

If we observe this output, we can see that we are not seeing “Constant number is initialized.” output at the time of definition. We are seeing this message only at the time of first access. It’s displaying that message only once right. Cool!.

如果观察到此输出,则可以看到没有看到“常量号已初始化”。 定义时的输出。 我们仅在首次访问时才看到此消息。 它仅一次显示该消息。 凉!。

NOTE:- We cannot use “lazy” modifier for var. It’s allowed to use only for val.

Example:-

注意:-我们不能对var使用“惰性”修饰符。 只允许用于val。

例:-

scala> lazy var a = 0:1: error: lazy not allowed here. Only vals can be lazylazy var a = 0     ^

var Vs lazy val (var Vs lazy val)

Here we will discuss some similarities and differences between val and lazy val constructs in Scala Language.

在这里,我们将讨论Scala语言中val和惰性val构造之间的一些异同。

Similarities between val and lazy val constructs:-

val和惰性val构造之间的相似之处:

  • Both are used to define constants or Immutable Data

    两者都用于定义常量或不可变数据
  • Both are evaluated only once.

    两者仅被评估一次。

Differences between val and lazy val constructs:-

val和惰性val构造之间的区别:

  • “val” is evaluated at the time of definition that means Eagerly.

    “ val”是在定义时评估的,表示“渴望”。
  • “lazy val” is evaluated only when we access it that means Lazily.

    仅当我们访问“惰性值”时才表示“惰性值”。

Scala val,var,lazy val和def构造In-Brief (Scala val,var,lazy val and def Constructs In-Brief)

In previous sections, we have discussed about Scala val,var,lazy val and def Constructs in-detail with some simple and useful examples. Here are some bullet points to remember.

在前面的部分中,我们通过一些简单而有用的示例详细讨论了Scala val,var,lazy val和def构造。 这里有一些要记住的要点。

  • “val” is used to define Immutable data. It’s evaluated only once at the time of definition.

    “ val”用于定义不可变数据。 在定义时仅评估一次。
  • “var” is used to define Mutable data. It’s evaluated only once at the time of definition.

    “ var”用于定义可变数据。 在定义时仅评估一次。
  • Both val and var are evaluated Eagerly.

    认真评估val和var。
  • “lazy val” is used to define Immutable data. It is evaluated only once when we access it for first time. That means it is evaluated Lazily.

    “惰性值”用于定义不可变数据。 当我们第一次访问它时,它只会被评估一次。 这意味着它被懒惰地评估。
  • “def” is used to define Methods or Functions. It is evaluated only when we access it and evaluated every-time we access it. That means it is evaluated Lazily.

    “ def”用于定义方法或函数。 仅当我们访问它时才对其进行评估,并在每次访问时对其进行评估。 这意味着它被懒惰地评估。

NOTE:-In Scala, “by-name” arguments are evaluated/computed every time we access them. As it’s very big concept, we will discuss about it in a separate post soon.

注意:-在Scala中,每次访问它们时,都会对“按名称”参数进行评估/计算。 由于它是一个非常大的概念,因此我们很快将在另一篇文章中进行讨论。

That’s it all about Scala val,var, def and lazy val constructs usage and evaluation. We will discuss some more Scala concepts in my coming posts.

这就是关于Scala val,var,def和lazy val构造用法和评估的全部内容。 我们将在我的后续文章中讨论更多Scala概念。

Please drop me a comment if you like my post or have any typo errors/issues/suggestions.

如果您喜欢我的帖子或有任何错字/问题/建议,请给我评论。

翻译自:

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

你可能感兴趣的文章
Windows autoKeras的下载与安装连接
查看>>
CMU Bomblab 答案
查看>>
微信支付之异步通知签名错误
查看>>
2016 - 1 -17 GCD学习总结
查看>>
linux安装php-redis扩展(转)
查看>>
Vue集成微信开发趟坑:公众号以及JSSDK相关
查看>>
技术分析淘宝的超卖宝贝
查看>>
i++和++1
查看>>
react.js
查看>>
P1313 计算系数
查看>>
NSString的长度比较方法(一)
查看>>
Azure云服务托管恶意软件
查看>>
My安卓知识6--关于把项目从androidstudio工程转成eclipse工程并导成jar包
查看>>
旧的起点(开园说明)
查看>>
生产订单“生产线别”带入生产入库单
查看>>
crontab导致磁盘空间满问题的解决
查看>>
java基础 第十一章(多态、抽象类、接口、包装类、String)
查看>>
Hadoop 服务器配置的副本数量 管不了客户端
查看>>
欧建新之死
查看>>
自定义滚动条
查看>>