scala 环境变量_Scala变量的范围

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 mark
	    println(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 percent
	    println(percent)
                    ^
one error found
Command exited with non-zero status 1


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

scala 环境变量

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值