scala 环境变量

Scala变量范围 (Scala variables scope)

Scope of the variable is the block of code until which the variable can be used within the scope of a variable. Any operation can be performed on it but accessing it outside the scope will give an error.

变量的范围是代码块,直到可以在变量的范围内使用变量为止。 可以对其执行任何操作,但是在范围之外访问它会产生错误。

Example:

例:

    def add(){var sum = 5+13}println(sum)

The above example will give an error as the scope of the sum is within the add() function and program are using it outside which is not allowed.

上面的示例将给出一个错误,因为总和的范围在add()函数之内,并且程序正在外部使用它,这是不允许的。

In Scala programming, there are three types of variables. They are:

在Scala编程中,存在三种类型的变量。 他们是:

  1. Fields

    领域

  2. Method Parameters

    方法参数

  3. Local variables

    局部变量

领域 (Fields)

A field is a variable that is defined in a class ie. variables defined inside the class body are known as fields. A field can be used by any method that is defined inside the class, also so you can use the object of the class to access the fields. Based on their access modifiers the object is allowed to access the fields. These access modifiers allow or disallow any member or object or subclass to access the specified field.

字段是在类(即,类)中定义的变量。 在类主体内部定义的变量称为字段。 字段可由类内部定义的任何方法使用,因此您也可以使用类的对象来访问字段。 根据其访问修饰符,允许对象访问字段。 这些访问修饰符允许或禁止任何成员,对象或子类访问指定字段。

For example:

例如:

For class, there might be a private variable. this private variable can be accessed from any other method of the class but the object of the class will not have access to it.

对于类,可能会有一个私有变量。 可以从该类的任何其他方法访问此私有变量,但该类的对象将无法访问它。

Code:

码:

class Student
{
var roll = 2343;
private var totalMarks = 500 // this cannot be accessed by the object
def printinfo(mark : Int)
{
println("The roll no. of student is "+roll)
println("Percentage is "+(mark/totalMarks))
}
}
object MyClass
{
def main(args:Array[String])
{
var s1 = new Student
println("call using . operator" + s1.roll)
// println("call using . operator" + s1.totalMarks) // will give error
s1.printinfo(345)
}
}

Output

输出量

call using . operator2343
The roll no. of student is 2343
Percentage is 0

方法参数 (Method parameters)

Parameters are those variables which are passed to the method while calling the method. These variables can be accessed only inside the method in which they are defined.

参数是在调用方法时传递给方法的那些变量。 这些变量只能在定义它们的方法内部访问。

For example:

例如:

    def add(a:Int , b:Int){
var sum = a+b
}
println(a)

As in this code, the variables a and b are parameters that are passed to the function add(). They cannot be used outside the block of the function. This means the println() line will give an error.

就像在此代码中一样,变量a和b是传递给函数add()的参数 。 不能在功能块之外使用它们。 这意味着println()行将给出错误。

Code:

码:

class Student
{
var roll = 2343;
private var totalMarks = 500 // this cannot be accessed by the object
def printinfo(mark : Int)
{
println("The roll no. of student is "+roll)
println("Percentage is "+(mark/totalMarks)) // marks is a parementer passed to method
}
def printmarks(){println(mark)
}
}
object MyClass
{
def main(args:Array[String])
{
var s1 = new Student
println("call using . operator" + s1.roll)
// println("call using . operator" + s1.totalMarks) // will give error
s1.printinfo(345)
}
}

Output

输出量


/home/jdoodle.scala:11: error: not found: value markprintln(mark)^
one error found
Command exited with non-zero status 1

局部变量 (Local variables)

Variables are those variables which are r define inside a class And if there scope also lies within the method in which they are defined.

变量是在类内定义的那些变量,如果范围也位于定义它们的方法之内。

For example:

例如:

    def add(a:Int , b:Int){
var sum = a+b
}
println(sum)

In this example, the code tries to access a variable outside its scope. This local variable cannot be used outside of the function.

在此示例中,代码尝试访问其范围之外的变量。 此局部变量不能在函数外部使用。

Code:

码:

class Student
{
var roll = 2343;
private var totalMarks = 500 // this cannot be accessed by the object
def printinfo(mark : Int)
{
println("The roll no. of student is "+roll)
val percent = ((mark/totalMarks)*100)
println("Percentage is "+(percent)) // marks is a parementer passed to method
}
def printmarks(){println(percent)
}
}
object MyClass
{
def main(args:Array[String])
{
var s1 = new Student
println("call using . operator" + s1.roll)
// println("call using . operator" + s1.totalMarks) // will give error
s1.printinfo(345)
}
}

Output

输出量


/home/jdoodle.scala:12: error: not found: value percentprintln(percent)^
one error found
Command exited with non-zero status 1

翻译自: https://www.includehelp.com/scala/scope-of-scala-variables.aspx

scala 环境变量

scala 环境变量_Scala变量的范围相关推荐

  1. scala方法中的变量_Scala变量,变量范围,字段变量,方法参数示例

    scala方法中的变量 Variables can be defined as the reserve space in memory to store the assigned values. Ba ...

  2. windows通过脚本批量设置环境变量(env、path)实战:java环境、scala环境、maven环境、gradle环境、nodejs、git等

    windows通过脚本批量设置环境变量(env.path)实战:java环境.scala环境.maven环境.gradle环境.nodejs.git等 目录

  3. scala方法中的变量_Scala中的变量

    scala方法中的变量 Scala变量 (Scala variables) A variable is named a reference to a memory location. The loca ...

  4. scala闭包 变量_Scala闭包,自由和绑定变量,匿名函数

    scala闭包 变量 A closure can be defined as the function whose return value depends on the value of one o ...

  5. js 执行环境 活动对象 变量对象 作用域链的理解

    看一下是知乎大神对于 js 执行环境 活动对象 变量对象 作用域链的解释 假设在全局环境下定义了函数pub()和变量pubvar: var pubvar = 1; function pub () {v ...

  6. scala的两种变量类型 var 和 val

    scala的两种变量类型 var 和 valscala的两种变量类型 var 和 val 在Java中,您可以这样声明新变量: String s = "hello"; int i ...

  7. 环境变量的变量名可以随便写么

    环境变量的变量名可以随便写 在配置path的时候能对应上就可以 path里是配置执行文件的路径 一般通过环境变量指向bin 例如下图    在export 时把定义的变量名输出

  8. 误删path等环境变量(系统变量/用户变量)

    误删path等环境变量(系统变量/用户变量) 切记重启或关机 1. 未关机或重启解决方法 首先按徽标键+R 打开运行,输入 regedit 打开注册表 在目录 HKEY_LOCAL_MACHINE\S ...

  9. Scala语法之常量变量和运算符

    Scala语法之常量变量和运算符 1. 背景 官网:https://www.scala-lang.org/ scala是什么 上述截图可知,scala是一门编程语言.静态语言需要编译才能运行,强类型语 ...

最新文章

  1. R语言使用ggplot2包使用geom_dotplot函数绘制分组点图(单色填充、分组颜色填充)实战(dot plot)
  2. Python手动编程实现斐波那契数列
  3. WINCE---内核(kernel)---内存架构(memory archtitecture)学习
  4. java linux download
  5. linux常见面试题
  6. 1000以内完数c语言程序_C语言经典面试题目及答案详解(二)
  7. Visual Studio下使用jQuery的10个技巧
  8. C 语言日期时间处理
  9. 沥青防水卷材行业调研报告 - 市场现状分析与发展前景预测
  10. java string 日期格式_Java 日期格式和String 转换
  11. ajax更换内容执行函数,在ajax成功调用另一个ajax函数
  12. python struct 45s_python32版本,struct.error: argument for 's' must be a bytes object - 小众知识...
  13. ecall 方法必须打包到系统模块中_基于SpringBoot+Vue+Mysql开发的进销存管理系统
  14. 爬楼梯 java_爬楼梯问题java实现
  15. bzoj2109 航空管制
  16. python常用的几个库_盘点Python常用的20个库
  17. 阿里云RDS云数据库的使用
  18. go中mgo操作数据库的一些示例
  19. 华为手机8.0.0怎么找到云相册_华为云相册下载-华为云相册预约 安卓版v1.0-PC6安卓网...
  20. http状态码查询表(转载)

热门文章

  1. 不思议迷宫c语言基础,不思议迷宫神龙收藏品一览
  2. java时间方法_JAVA处理日期时间常用方法
  3. mybatis mysql selectkey_Mybatis示例之SelectKey的应用
  4. java io流读取txt文件_Java使用IO流读取TXT文件
  5. php 栈实现历史记录后退,栈:如何实现浏览器的前进和后退功能
  6. CENTOS5下VSFTPD的设置
  7. Android_内存泄露
  8. 多IDC GSLB的部署
  9. PHP-Manual的学习----【语言参考】----【类型】-----【对象】
  10. 优化Android应用内存的若干方法