1.建立多行字符串,只要把多行字符串放在3个双引号间("""...""")便可。这是Scala对于here document,或者叫heredoc的支持。这里咱们建立一个三行长的字符串:java
MultiLine.scala安全
val str = """Hello world! How are you! Good morning!""" println(str)
输出以下:app
Hello world! How are you! Good morning! 请按任意键继续. . .
2.运算符优先级ide
Scala没有定义运算符的优先级,但它定义了方法的优先级。函数
方法名的第一个字符决定了它的优先级。若是表达式里面有两个具备相同优先级的字符,那么左边的运算符优先级更高。下面从低到高列出了首字符的优先级:post
全部字母 | ^ & < > = ! : + - * / % 全部其余特殊字符
举例以下:url
Complex.scalaspa
class Complex(val real:Int, val imag:Int) { def +(operand:Complex):Complex={ println("Calling +") new Complex(real+operand.real , imag+operand.imag) } def *(operand:Complex):Complex={ println("Calling *") new Complex(real*operand.real - imag*operand.imag , real*operand.imag + imag*operand.real ) } override def toString():String = { real + (if(imag<0) ""else "+")+ imag + "i" } } object Complex extends App { val c1 = new Complex(1,4) val c2 = new Complex(2,-3) val c3 = new Complex(2,2) // val sum = c1 + c2 // println("(" + c1 + ") + (" + c2 + ")=" + sum ) println(c1+c2*c3) }
运行结果以下:scala
Calling *
Calling +
11+2i
3. 类继承code
在scala里,继承一个基类跟Java的作法很类似,只是多了两点限制:
(1)重写方法须要override关键字
(2)只有主构造函数才能往基类构造函数中传参数
下面是继承一个基类的例子:
Vehicle.scala
class Vehicle(val id:Int, val year:Int) { override def toString(): String = "ID:" + id + " Year:" + year } class Cars(override val id:Int, override val year:Int, var fuelLevel:Int)extends Vehicle(id,year){ override def toString(): String = super.toString()+ " Fuel Level:" + fuelLevel } object Cars extends App { val car = new Cars(1, 2010, 100) println(car) }
程序运行结果以下:
ID:1 Year:2010 Fuel Level:100
4. Scala中的static
Scala中没有静态字段和静态方法。静态字段和静态方法会打破Scala所支持的完整的面向对象模型。不过,scala也是彻底支持类一级的属性和操做的。这就是伴生对象的做用。
举例以下:
Marker.scala
class Marker private(val color:String) { override def toString():String = "marker color "+color } object Marker extends App{ private val markers = Map( "red" -> new Marker("red"), "blue" -> new Marker("blue"), "green" -> new Marker("green") ) def primaryColors = "red, green, blue" def apply(color:String) = if(markers.contains(color)) markers(color) else null println("Primary colors are:" + Marker.primaryColors) println(Marker("blue")) println(Marker("red")) }
程序运行结果以下:
Primary colors are:red, green, blue
marker color blue
marker color red
5. Option类型
Scala想让意图更清晰的表达出来,确实有时候咱们就须要没有结果。Scala以一种类型安全的方式作到这一点:它使用Option[T]类型。举例以下:
OptionExample.scala
object OptionExample extends App{ def commentOnPractice(input:String) = { //rather than returning null if(input == "test") Some("good") else None } for(input <- Set("test","hack")){ val comment = commentOnPractice(input) println("input "+input+"comment "+comment.getOrElse("Found no comments")) } }
程序运行结果以下:
input testcomment good
input hackcomment Found no comments
将类型显式声明为Option[String],Scala会强制咱们检查实例的不存在。如此一来,就不太可能由于没有检查null引用而抛出NullPointerException。调用返回Option[T]的getOrElse()方法,能够主动的应对结果不存在(None)的情形。
6.方法返回类型推演
除了推演变量的类型,Scala也会尝试推演方法返回值的类型。不过,这有个陷阱,推演会依赖于方法如何定义。若是用等号(=)定义方法,Scala就能够推演返回类型。不然,它就假设方法是一个void方法。
7. 隐式类型转换
举例以下:
import java.util._ class DateHelper(number:Int) { def days(when:String):Date = { var date = Calendar.getInstance() when match { case DateHelper.ago => date.add(Calendar.DAY_OF_MONTH, -number) case DateHelper.from_now => date.add(Calendar.DAY_OF_MONTH, number) case _ => date } date.getTime(); } } object DateHelper extends App{ val ago = "ago" val from_now = "from_now" implicit def convertInt2DateHelper(number:Int) = new DateHelper(number) val past = 2 days ago val appointment = 5 days from_now println(past) println(appointment) }
程序运行结果以下:
Fri Mar 13 16:49:06 CST 2015
Fri Mar 20 16:49:06 CST 2015
7. 使用Set,合并Set集
若是须要将两个Set合并成一个新的Set,能够用++():
scala> val feeds1 = Set("blog.toolshed.com","pragdave.pragprog.com","pragmactic- osxer.blogspot.com","vita-contemplativa.blogspot.com") feeds1: scala.collection.immutable.Set[String] = Set(blog.toolshed.com, pragdave .pragprog.com, pragmactic-osxer.blogspot.com, vita-contemplativa.blogspot.com) scala> val feeds2 = Set("blog.toolshed.com","martinfowler.com/bliki") feeds2: scala.collection.immutable.Set[String] = Set(blog.toolshed.com, martinfo wler.com/bliki) scala> val mergedFeeds = feeds1 ++ feeds2 mergedFeeds: scala.collection.immutable.Set[String] = Set(pragdave.pragprog.com, pragmactic-osxer.blogspot.com, vita-contemplativa.blogspot.com, blog.toolshed.c om, martinfowler.com/bliki) scala> println("# of merged feeds:" + mergedFeeds.size) # of merged feeds:5
Set至多持有每一个元素一次,从输出里能够看到这一点,在合并后的Set里,两个Set里公共的元素只存储一次:
若是想在每一个元素前面加上“http://",能够用map()方法:
scala> val urls = feeds1 map("http://"+_) urls: scala.collection.immutable.Set[String] = Set(http://blog.toolshed.com, http://pragdave.pragprog.com, http://pragmactic-osxer.blogspot.com, http://vita-con templativa.blogspot.com)
若是准备对Set集进行迭代,一次一个的进行刷新,能够用内建的迭代器foreach(),像这样:
scala> feeds1 foreach{ feed => println(" Refreshing " + feed)} Refreshing blog.toolshed.com Refreshing pragdave.pragprog.com Refreshing pragmactic-osxer.blogspot.com Refreshing vita-contemplativa.blogspot.com
8. 用Scala实现集合中相邻元素间的差值
如何计算同一集合中元素两两之间的差值,即求集合(a,b,c,d)中的(b-a,c-b,d-c)
实现方法以下:
scala> val l1 = 1 to 10 toList warning: there were 1 feature warning(s); re-run with -feature for details l1: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> val l2 = l1.tail l2: List[Int] = List(2, 3, 4, 5, 6, 7, 8, 9, 10) scala> l1.zip(l2).map(p=>(p._2-p._1)) res1: List[Int] = List(1, 1, 1, 1, 1, 1, 1, 1, 1)
代码含义解释以下: