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 } }