scala 连接符_Scala标识符示例教程

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)

  1. AlphaNumeric Identifiers

    字母数字标识符
  2. 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数据类型的合法标识符。

  3. Operator identifiers

    操作员标识符
  4. 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.

    在上面的示例中,+,-是运算符标识符 ,用于添加和减去变量并输出结果。

  5. Mixed Identifiers

    混合标识符
  6. 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

    输出
    50

    In the above example marks_+ is a mixed identifier.

    在上面的示例中,marks_ +是混合标识符。

  7. Literal identifiers

    文字标识符
  8. 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 连接符

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值