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

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

scala语言示例

Scala var关键字 (Scala var keyword)

The var Keyword in scala is used to declare variables. As Scala does not require explicit declaration of , var keyword is used. The variable declared using the var keyword as mutable variables.

scala中var关键字用于声明变量。 由于Scala不需要显式声明的使用var关键字 。 使用var关键字声明的变量为可变变量。

Mutable Variables are those variable whose value can be changed in the program, these are used when you need variables which are updated as per the logic of the program. Example of some mutable variables are counters, loop variables, sum, etc.

可变变量是可以在程序中更改其值的那些变量,当您需要根据程序逻辑进行更新的变量时,将使用这些变量。 某些可变变量的示例包括计数器,循环变量,总和等。

Syntax to define mutable variables in Scala using var keyword:

使用var关键字在Scala中定义可变变量的语法:

var I = 102;    var H : string = "IncludeHelp";

Syntax explanation, in the first on we have declared a variable I using var keyword. This can be changed in future. The value is 102 so scala will itself make it an int type. In the second one we have explicitly declared the data type of the variable. This is a good practice and you know the limitation of the initialization. It declares the var H as a string using variable. The value will be strictly string and if we stored a number in it will be stored as a string.

语法说明,首先,我们使用var keyword声明了变量I。 将来可以更改。 值是102,因此scala本身会将其设置为int类型。 在第二篇文章中,我们明确声明了变量的数据类型。 这是一个好习惯,您知道初始化的局限性。 它使用变量将var H声明为字符串。 该值将严格为字符串,如果我们在其中存储了数字,则将其存储为字符串。

Using var keyword you can define variables of all data types in Scala. The value of variables declared using the var keyword can be changed at any point in the program.

使用var关键字,您可以在Scala中定义所有数据类型的变量。 使用var关键字声明的变量的值可以在程序的任何位置更改。

Scala程序演示var关键字示例 (Scala program to demonstrate example of var keyword)

// Program that displays usage of var keyword in Scalaobject VarKeyword {
def main(args: Array[String]) {
var myVar = 52;//Variable Initialized with value 52 print("Value of my myVar =" + myVar + "\n") myVar = myVar + 6; // Value changes to 52+6 print("Changed Value of myVar = " + myVar ) } }

Output

输出量

Value of my myVar =52Changed Value of myVar = 58

Example explanation:

示例说明:

The code displays use of var keyword in real world program. The variable myVar's value changes in the program.

该代码显示了在实际程序中var关键字的使用。 变量myVar的值在程序中更改。

翻译自:

scala语言示例

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

你可能感兴趣的文章
PHP基础
查看>>
UVa 11488 超级前缀集合(Trie的应用)
查看>>
Django 翻译与 LANGUAGE_CODE
查看>>
[转]iOS教程:SQLite的创建数据库,表,插入查看数据
查看>>
【转载】OmniGraffle (一)从工具栏开始
查看>>
初识ionic
查看>>
java 中打印调用栈
查看>>
开发 笔记
查看>>
ajax跨域,携带cookie
查看>>
阶段3 2.Spring_01.Spring框架简介_03.spring概述
查看>>
阶段3 2.Spring_02.程序间耦合_1 编写jdbc的工程代码用于分析程序的耦合
查看>>
阶段3 2.Spring_01.Spring框架简介_04.spring发展历程
查看>>
阶段3 2.Spring_02.程序间耦合_3 程序的耦合和解耦的思路分析1
查看>>
阶段3 2.Spring_02.程序间耦合_5 编写工厂类和配置文件
查看>>
阶段3 2.Spring_01.Spring框架简介_05.spring的优势
查看>>
阶段3 2.Spring_02.程序间耦合_7 分析工厂模式中的问题并改造
查看>>
阶段3 2.Spring_02.程序间耦合_4 曾经代码中的问题分析
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_2 spring中的Ioc前期准备
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_4 ApplicationContext的三个实现类
查看>>
阶段3 2.Spring_02.程序间耦合_8 工厂模式解耦的升级版
查看>>