Point 1:
package com.scala.learn
object Test02 {
def main(args: Array[String]) {
TestObject.func02()
val u = new User(234,423)
println(u.height)
val u2 = new User()
println(u2.height)
val u3 = new User(3,5,"gaga")
println(u3.name)
println(fac(5))
}
def fac(n : Int) : Int=
if (n <= 0) 1
else n * fac(n-1)
}
Point 2:
package com.scala.learn
object TestCollection {
def main(args: Array[String]) {
var t = List(1,2,3,5,5)
println("---001--"+t(2))
// map 个位置相加 函数编程
println(t.map(a => {print("***"+a); a+2}));
println(t.map(_ + 2))
var t2 = t.+:("test")//添加元素
println(t2)
// println(t.::("test"))
// println("test"::t)
println(6::t2)
println(t2.::(6))
println(List(6).:::(t2))
// println((t2):::List(6))
// println(t2)
// t2 = t:::6::Nil //组成新的List t作为一个元素
// println(t2)
t2.foreach(a=>print("---+++"+a))
println("/--***---"+t.distinct)
println(t.sortBy{ x => -x })
println("---+++++********************Slice"+t.slice(0, 2))
println("-*--*--*--*--*--*--*--*--*-")
for(temp<-t2){
print(temp)
}
println("-*--*--*--*--*--*--*--*--*-")
for(i <- 0 until t2.length){
println(i)
println(t2(i))
}
println("-*--*--*--*--*--*--*--*--*-")
println(t./:(0)({
(sum,num)=>sum-num
}))
// 1,2,3,5,5
println(t.reduce( _-_ ))
println(t.filter ( _>3 ))
println(t.take(3))
// println("-*--*--*--*--*--*--*--*--*-")
// println(t.foldLeft(0)((sum,num)=>{
// print(sum+"--"+num+" ");
// sum-num;
// }))
// println("-*--*--*--*--*--*--*--*--*-")
// println(t.map(v =>v+2))
println("-*--*--*--*--*--*--*--*--*-")
// 元组
var tuple01 = (1,2,3,5)
println(tuple01._1)
println(tuple01._4)
var list = List(("Aaron",100),("xuruyun",100),("liangyongqi",50))
val temp = list.sortBy(x => (-x._2,x._1))
println(temp)
}
}
Point 3:
package com.scala.learn
trait Listen {
val name: String
def listen() = {
println("You friend " + name + " is listening")
}
}
trait Read {
val name: String
def read() = {
println("You friend " + name + " is reading")
}
}
trait Speak {
val name: String
def speak() = {
println("You friend " + name + " is speaking.")
}
}
class Human(val name: String) {
def listen() = {
println(name + " is listening.")
}
}
class Animal(val name: String)
class Cat(override val name: String) extends Animal(name: String) with Speak with Listen with Read {
override def toString(): String = " hello " + name + "! "
}
object TestFriend {
def main(args: Array[String]) {
// val f1 = new Listen();//java 接口很像 不能直接构造
val h1 = new Human("gaga")
h1.listen()
val h2 = new Human("xuruyun") with Speak
h2.speak()
//这样scat就是Friend了
val scat = new Cat("hello kitty")
scat.listen()
scat.speak()
println(scat)
}
}
Point 4:
package com.scala.learn
import java.sql.{ResultSet, DriverManager}
import scala.util.parsing.json.{JSONFormat, JSONObject, JSONArray, JSON}
object ScalaMysql {
def main(args: Array[String]) {
// create database connection
val dbc = "jdbc:mysql://spark003:3306/test?user=root&password=123123"
classOf[com.mysql.jdbc.Driver]
val conn = DriverManager.getConnection(dbc)
// do database insert
try {
val prep = conn.prepareStatement("INSERT INTO blog (title, body) VALUES (?, ?) ")
prep.setString(1, "Nothing great was ever achieved without enthusiasm.")
prep.setString(2, "Ralph Waldo Emerson")
prep.executeUpdate
}
finally {
conn.close
}
}
}
Point 5:
package com.scala.learn
import java.util._
import scala.Immutable
object TestArrayList {
def main(args: Array[String]) {
// var val
// java === scala 变 声明
var arr = new ArrayList[Any]
arr.add(123)
arr.add("gaga")
println(arr)
println(arr.get(0))
val it = arr.iterator();
while (it.hasNext()) {
println(it.next());
}
val stu1 = new Student1(1,"name",100)
println(stu1)
}
}