- Scala Tutorial
- Scala - Home
- Scala - Overview
- Scala - Environment Setup
- Scala - Basic Syntax
- Scala - Data Types
- Scala - Variables
- Scala - Classes & Objects
- Scala - Access Modifiers
- Scala - Operators
- Scala - IF ELSE
- Scala - Loop Statements
- Scala - Functions
- Scala - Closures
- Scala - Strings
- Scala - Arrays
- Scala - Collections
- Scala - Traits
- Scala - Pattern Matching
- Scala - Regular Expressions
- Scala - Exception Handling
- Scala - Extractors
- Scala - Files I/O
- Scala Useful Resources
- Scala - Quick Guide
- Scala - Useful Resources
- Scala - Discussion
Scala - 赋值运算符
尝试以下示例程序来了解 Scala 编程语言中可用的所有按位运算符。
例子
object Demo { def main(args: Array[String]) { var a = 10; var b = 20; var c = 0; c = a + b; println("c = a + b = " + c ); c += a ; println("c += a = " + c ); c -= a ; println("c -= a = " + c ); c *= a ; println("c *= a = " + c ); a = 10; c = 15; c /= a ; println("c /= a = " + c ); a = 10; c = 15; c %= a ; println("c %= a = " + c ); c <<= 2 ; println("c <<= 2 = " + c ); c >>= 2 ; println("c >>= 2 = " + c ); c >>= 2 ; println("c >>= 2 = " + c ); c &= a ; println("c &= a = " + c ); c ^= a ; println("c ^= a = " + c ); c |= a ; println("c |= a = " + c ); } }
将上述程序保存在Demo.scala中。以下命令用于编译和执行该程序。
命令
\>scalac Demo.scala \>scala Demo
输出
c = a + b = 30 c += a = 40 c -= a = 30 c *= a = 300 c /= a = 1 c %= a = 5 c <<= 2 = 20 c >>= 2 = 5 c >>= 2 = 1 c &= a = 0 c ^= a = 10 c |= a = 10
scala_operators.htm