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編程中,存在三種類型的變量。 他們是:
Fields
領域
Method Parameters
方法參數
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 objectdef 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 Studentprintln("call using . operator" + s1.roll)// println("call using . operator" + s1.totalMarks) // will give errors1.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 objectdef 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 Studentprintln("call using . operator" + s1.roll)// println("call using . operator" + s1.totalMarks) // will give errors1.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 objectdef 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 Studentprintln("call using . operator" + s1.roll)// println("call using . operator" + s1.totalMarks) // will give errors1.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变量的范围的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 旅行商问题
- 下一篇: 密码学哈希函数_哈希函数在密码学中的应用