您的位置:首页 > 其它

Programming In Scala Reading Note 4

2014-06-25 16:47 369 查看

Basic Types and Operations

Something new according to Java

1 + 2 跟 (1).+(2) 是一样的。

val str = "Hello world"

str indexOf 'o' 跟 str.indexOf('o')是一样的

indexOf有两种形式的参数,int和str,其中这个int就是用来确定是否存在一个char的。。。。

Any method can be an operator

这句话应该这么理解,str.indexOf('o')中的indexOf应该叫做method,而str indexOf 'o'中的indexOf才是一个operator,跟+一样。。。。

一元(unary)操作符

前缀:一元操作符的方法定义按照:unary_(m),其中这个m只能是+,-,!,~中的一个

后缀:也就是没有参数的方法,这里有一个约定,如果一个方法没有副作用的话,这个方法可以省略掉(),如String的toLowerCase方法;如果有副作用的话,这个()是不能省略的,如println()

  The convention is that you include parentheses if the method has side effects, such as println(), but you can leave them off if the method has no side effects, such as toLowerCase invoked on a String

str.toLowerCase()

str.toLowerCase

str toLowerCase()

str toLowerCase

他们都是一样的。。。。

有点问题,不知道为什么他们不能放倒一起

==

scala的==跟java的==不同

Java中的==用于primitive类型的时候,比较的是值,scala的方法是跟Java的一样

1 == 1; true == false; 等等

Java中==用于引用类型的时候比较的是引用类型的相等性,是否指向的是同一个对象(the two variables point to the same object on the JVM’s heap)。

OK SOMETHING WAIT FOR PREPAIR

val set1 = scala.collection.immutable.Set(1, 2, 3)

val set2 = scala.collection.mutable.Set(1, 2, 3)

set1 == set2 //true!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: