1.scala shell命令
scala> :help
All commands can be abbreviated, e.g., :he instead of :help.
:edit <id>|<line> edit history
:help [command] print this summary or command-specific help
:history [num] show the history (optional num is commands to show)
:h? <string> search the history
:imports [name name ...] show import history, identifying sources of names
:implicits [-v] show the implicits in scope
:javap <path|class> disassemble a file or class name
:line <id>|<line> place line(s) at the end of history
:load <path> interpret lines in a file
:paste [-raw] [path] enter paste mode or paste a file
:power enable power user mode
:quit exit the interpreter
:replay [options] reset the repl and replay all previous commands
:require <path> add a jar to the classpath
:reset [options] reset the repl to its initial state, forgetting all session entries
:save <path> save replayable session to a file
:sh <command line> run a shell command (result is implicitly => List[String])
:settings <options> update compiler options, if possible; see reset
:silent disable/enable automatic printing of results
:type [-v] <expr> display the type of an expression without evaluating it
:kind [-v] <expr> display the kind of expression's type
:warnings show the suppressed warnings from the most recent line which had any
2.scala基本類型

3.常用特殊字符
\n 換行符,其Unicode編碼為 (\u000A)
\b 回退符,其Unicode編碼為 (\u0008)
\t tab制表符 ,其Unicode編碼(\u0009)
\” 雙引號,其Unicode編碼為 (\u0022)
\’ 單引號,其Unicode編碼為 (\u0027)
\ 反斜桿,其Unicode編碼為(\u005C)
4.函數

5.類
//采用關鍵字class定義
class Person {
//類成員必須初始化,否則會報錯
//這里定義的是一個公有成員
var name:String=null
}
附錄:
import java.sql.{ Connection, DriverManager }
import java.util.{Date, Locale}
import java.text.DateFormat
import java.text.DateFormat._
object Mysql extends App {
// def main(args: Array[String]): Unit = {
// 訪問本地MySQL服務器,通過3306端口訪問mysql數據庫
val url = "jdbc:mysql://localhost:3306/mysql"
//驅動名稱
val driver = "com.mysql.jdbc.Driver"
//用戶名
val username = "root"
//密碼
val password = "1"
//初始化數據連接
var connection: Connection = _
try {
//注冊Driver
Class.forName(driver)
//得到連接
connection = DriverManager.getConnection(url, username, password)
val statement = connection.createStatement
//執行查詢語句,並返回結果
val rs = statement.executeQuery("SELECT host, user FROM user")
//打印返回結果
while (rs.next) {
val host = rs.getString("host")
val user = rs.getString("user")
println("host = %s, user = %s".format(host, user))
}
} catch {
case e: Exception => e.printStackTrace
}
//關閉連接,釋放資源
connection.close
//for循環
var myArray: Array[String] = new Array[String](10);
for (i <- 0 until myArray.length) {
myArray(i) = "value is: " + i;
}
for (value: String <- myArray) {
println(value);
}
for (value: String <- myArray if value.endsWith("5")) {
println(value);
}
val now = new Date
val df = getDateInstance(LONG, Locale.FRANCE)
println(df format now)
//}
val greetStrings =new Array[String](3)
greetStrings(0)="Hello"
greetStrings(1)=","
greetStrings(2)="world!\n"
greetStrings.update(0,"Hello")
greetStrings.update(1,",")
greetStrings.update(2,"world!\n")
for(i <- 0 to 2)
print(greetStrings(i))
val oneTwo = List(1,2)
val threeFour = List(3,4)
val oneTwoThreeFour=oneTwo ::: threeFour
println (oneTwo + " and " + threeFour + " were not mutated.")
println ("Thus, " + oneTwoThreeFour + " is a new list")
//幾種循環
def printArgs ( args: Array[String]) : Unit ={
var i=0
while (i < args.length) {
println (args(i))
i+=1
}
}
def printArgs1 ( args: Array[String]) : Unit ={
for( arg <- args)
println(arg)
}
def printArgs2 ( args: Array[String]) : Unit ={
args.foreach(println)
}
//類與對象
class ChecksumAccumulator{
private var sum=0
def add(b:Byte) :Unit = sum +=b
def checksum() : Int = ~ (sum & 0xFF) +1
}
def gcdLoop (x: Long, y:Long) : Long ={
var a=x
var b=y
while( a!=0) {
var temp=a
a=b % a
b = temp
}
b
}
}
