scala 连接符
The names of variables, classes, objects and methods are collectively called Identifiers. Scala identifiers are case sensitive. We have been using identifiers all through the tutorial series. Let us now see them in more detail.
变量,类,对象和方法的名称统称为标识符 。 Scala标识符区分大小写。 在整个教程系列中,我们一直在使用标识符。 现在让我们更详细地了解它们。
Scala标识符类型 (Scala Identifiers Types)
- AlphaNumeric Identifiers 字母数字标识符
Alphanumeric identifiers start with a letter or underscore that can be further followed by digits, letters or underscores. The $ character is reserved keyword and cannot be used as identifiers.
字母数字标识符以字母或下划线开头,后跟数字,字母或下划线。 $字符是保留关键字,不能用作标识符。
For example sage,sname,_id etc.. are valid identifiers.
例如,sage,sname,_id等是有效的标识符。
$age, -id, 45name are invalid identifiers.
$ age,-id,45name是无效的标识符。
object alphanumeric { def main(args:Array[String]) { val sage = 8 ; val sname = "Rob"; println("Age :"+sage) println("Name:"+sname) } }
When executed main method, it produces below output.
执行main方法时,将产生以下输出。
Age :8 Name:Rob
Here the variables sage and sname are legal identifiers of Integer and String data types.
在这里,变量sage和sname是Integer和String数据类型的合法标识符。
- Operator identifiers 操作员标识符
An operator consists of one or more operator characters. Operator characters include +, :, ?, ~ or # .
一个运算符由一个或多个运算符组成。 运算符包括+,:,?,〜或#。
For example + , – , => :::
例如+,–,=> :::
object Operator { def main(args:Array[String]) { val a =10 ; val b=42; val c = a + b ; val d = b -a ; println("Result of + operator identifier: "+c); println("Result of - identifier: "+d); } }
Output:
输出:
Result of + operator identifier: 52 Result of - identifier: 32
In the above example +, – are the operator identifiers which adds and subtracts the variables and prints the result.
在上面的示例中,+,-是运算符标识符 ,用于添加和减去变量并输出结果。
- Mixed Identifiers 混合标识符
A mixed identifier contains alphanumeric character followed by underscore and an operator identifier.
混合标识符包含字母数字字符,后跟下划线和运算符。
Example unary_+ , myVar_=
示例unary_ +,myVar_ =
object Mixed { def main(args:Array[String]) { val marks_+ = 50; println(marks_+); } }
Output:
50输出 :
50In the above example marks_+ is a mixed identifier.
在上面的示例中,marks_ +是混合标识符。
- Literal identifiers 文字标识符
A literal identifier is an arbitrary string enclosed with back ticks (`……`) .
文字标识符是用反引号(`……`)括起来的任意字符串。
For example; `age` `name` etc are literal identifiers
例如; `age`name`等是文字标识符
object Literal { def main(args:Array[String]) { val `x` = 10; val `message` = "Hello" println(`x` + "\n" + `message`); } }
Output:
10
Hello输出:
10
你好Here the variables x and message are enclosed with back ticks indicating the literal identifiers.
这里,变量x和message用反斜线括起来,指示文字标识符。
That’s all for a quick roundup on Scala identifiers, we will look into regular expressions in scala in next tutorial.
这就是对Scala标识符的快速汇总,我们将在下一个教程中研究scala中的正则表达式。
翻译自: https://www.journaldev.com/8170/scala-identifiers-example-tutorial
scala 连接符