scala(一)

1、变量和常量

scala> var a:String="hello"
a: String = hello

scala> var a="ji"
a: String = ji

scala> var a=1
a: Int = 1

scala> var a:Int=1--->可以定义变量的时候定义类型
a: Int = 1

scala> var a:Int=2
a: Int = 2

scala> var a="hello"
a: String = hello

scala> var a:String="world"
a: String = world

scala> var a:Integer=22---->integer兼容int类型
a: Integer = 22

scala> val a = 20;---->常量,不可更改
a: Int = 20

scala> a=100
<console>:12: error: reassignment to val
       a=100
        ^

scala> var  a = 10;---->变量可更改
a: Int = 10

scala> a = 200
a: Int = 200

scala> 1 to 10
res3: scala.collection.immutable.Range.Inclusive = Range 1 to 10

scala> var a = 1 to 10--->定义一个数组类型
a: scala.collection.immutable.Range.Inclusive = Range 1 to 10
--->类型转换
scala> 1.to
to               toByte   toDegrees   toFloat       toInt    toOctalString   toShort
toBinaryString   toChar   toDouble    toHexString   toLong   toRadians       toString

scala> 1.tostring
<console>:12: error: value tostring is not a member of Int
       1.tostring
         ^

scala> 1.Tostring
<console>:12: error: value Tostring is not a member of Int
       1.Tostring
         ^

scala> 1.toString
res6: String = 1

scala> 1.toDouble
res7: Double = 1.0

scala> 1.toByte
res8: Byte = 1

2、操作符重载

scala> 1+2
res9: Int = 3

scala> 1.+(2)--->通过.调用方法,括号里面相当于调用的加法方法的参数
res10: Int = 3

scala> 1.-(2)
res11: Int = -1

scala> 1.*(2)
res12: Int = 2

scala> 1.%(2)
res13: Int = 1

scala> 1.to(10)
res14: scala.collection.immutable.Range.Inclusive = Range 1 to 10

3、scala没有++,–,只有+=

scala> var a = 100;
a: Int = 100

scala> a+=1;

scala> a
res25: Int = 101

scala> a+=100;

scala> a
res27: Int = 201

4、导入包

scala> import scala.math._
import scala.math._       --->下划线表示通配符
scala> min(1,2)---->Scala函数没有对象,方法有对象,方法通过对象调用
res29: Int = 1


scala> 1.toString  --->对于没有参数的方法可以省略括号
res30: String = 1

scala> 1 toString  --->运算符的方式
<console>:15: warning: postfix operator toString should be enabled
by making the implicit value scala.language.postfixOps visible.
This can be achieved by adding the import clause 'import scala.language.postfixOps'
or by setting the compiler option -language:postfixOps.
See the Scaladoc for value scala.language.postfixOps for a discussion
why the feature should be explicitly enabled.
       1 toString
         ^
res31: String = 1


scala> 1.toString()    --->方法的形式
res32: String = 1

5、apply方法

scala> "hello"(0)  --->隐含调用apply方法
res35: Char = h

scala> "hello".apply(0)---->等价于上面那个
res36: Char = h

scala> "hello".apply(1)
res37: Char = e

scala> val a = "hello"
a: String = hello

scala> a(0)
res38: Char = h

6、条件表达式

scala> var x = 3;
x: Int = 3

scala> var s = 0;
s: Int = 0

scala> if (x>1) s=1 else s =  -1;

--->等价于上面的表达式
scala> s
res43: Int = 1

scala> var b = if(x>1) 1 else -1; ---->scala的条件表达式都是有值的
b: Int = 1

scala> val c = if(x>1) 1 else "hello"
c: Any = 1  ---->any类型是int类型和string类型的父类
scala>

6、类型转换

scala> "1000". toInt
res45: Int = 1000

7、unit类(赋值语句没有值,用unit类表示)

scala> val y = (s=1);
y: Unit = ()
scala> val y:Unit=();  ---->可以声明为unit类型,相当于void
y: Unit = ()

8、Scala没有switch语句

scala> if(x>0) 1  --->如何没有定义类型,会自动分配一个anyval类型
res46: AnyVal = 1

9、进入粘贴模式(ctr+d结束粘贴模式)

scala> :paste
// Entering paste mode (ctrl-D to finish)

if(x>1){1}
else -1

// Exiting paste mode, now interpreting.

res48: Int = 1

scala>

10、Java—->.java–javac—->.class——>程序
打印输出

scala> print(1)
1
scala> println(2)
2

scala> printf("name is %s, age=%d","tong",12)
name is tong, age=12
scala>


从终端读取
scala> val name = readLine("请输入密码:");
<console>:14: warning: method readLine in trait DeprecatedPredef is deprecated (since 2.11.0): use the method in `scala.io.StdIn`
       val name = readLine("请输入密码:");
                  ^
请输入密码:name: String = 12323

scala>
posted @ 2017-11-16 12:01  crr121  阅读(159)  评论(0编辑  收藏  举报