http://www.yiibai.com/scala/
/**
* Created by Administrator on 2015/9/16.
*/
object HelloWorld {
def main(args: Array[String]) {
println("laizi helloworld")
}
}
/**
* Created by Administrator on 2015/10/21.
*/
object Collect {
def main(args: Array[String]) {
// Define List of integers.
val l = List(1,2,3,4)
println(l)
// Define a set.
var s = Set(1,3,5,7)
println(s)
// Define a map.
val m= Map("one" -> 1, "two" -> 2, "three" -> 3)
println(m.toString())
println(m)
// Create a tuple of two elements.
val t = (10, "Scala")
println(t)
// Define an option
val o:Option[Int] = Some(5)
println(o)
}
}
object For {
def main(args: Array[String]) {
var a = 0;
val numList = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// for loop execution with a yield
var retVal = for {a <- numList
if a != 3; if a < 8
} yield a
// Now print returned values using another loop.
for (a <- retVal) {
println("Value of a: " + a);
}
}
}
class Person {
private var name=null;
var age=0;
protected var city=null;
protected def f() {
println("f")
}
def d() {
println("d")
}
def c() {
println("c")
}
def addInt( a:Int, b:Int ) : Int = {
var sum:Int = 0
sum = a + b
return sum
}
}
import scala.util.control.Breaks
/**
* Created by wjh on 2015/10/21.
*/
object Main {
def printMe( ) : Unit = {
println("Hello, Scala!")
}
def testBreak(){
var a = 0;
val numList = List(1,2,3,4,5,6,7,8,9,10);
val loop = new Breaks;
loop.breakable {
for( a <- numList){
println( "Value of a: " + a );
if( a == 4 ){
loop.break;
}
}
}
println( "After the loop" );
}
//基本语法
def main (args: Array[String]) {
printMe();
println("xxx");
println("data")
println(Main.toString)
//可变变量
var myVar : String = "Foo可变变量"
myVar="fuck";
println(myVar)
//不可变变量
val myVal : String = "Foo不可变变量"
//myVal="fuck";
println(myVal)
var myVar1 = 10;
val myVal1 = "Hello, Scala!";
println(myVar1)
println(myVal1)
val (myVar2, myVar3) = Pair(40, "Foo多重任务")
// val (myVar2: Int, myVar3: String) = Pair(40, "Foo多重任务")
println(myVar2)
println(myVar3)
var person=new Person();
person.d();
person.c();
println(person.addInt(12,12));
//调用java方法
println(Math.PI)
println(Thread.currentThread().getName);
System.out.println("来自java")
var name=this.getClass.getSimpleName();
println(name)
var cls:String=this.getClass.getClassLoader.getClass.getName();
println(cls)
println(System.getProperties());
//http://www.yiibai.com/scala/scala_for_loop.html
var i=0;
for(i<- 0 to 15)
{
println("fuck"+i);
}
for( a <- 1 until 10){
println( "Value of a: " + a );
}
i=5;
while(i>0)
{
println("ok"+i);
i=i-1;
}
testBreak();
}
override def toString: String
=super.toString
}
import java.io.{FileNotFoundException, FileReader, IOException}
import java.util.Date
object TestName {
def main(args: Array[String]) {
delayed(time());
printInt(b = 5, a = 7);
for (i <- 1 to 10)
println("Factorial of " + i + ": = " + factorial(i))
var palindrome = "Dot saw I was Tod";
var len = palindrome.length();
println("String Length is : " + len);
printStrings("Hello", "Scala", "Python");
println("Returned Value : " + addInt());
val str1: String = "Hello, "
val str2: String = "Scala!"
println("str1 + str2 = " + strcat(str1)(str2))
val date = new Date
log(date, "message1")
log(date, "message2")
Thread.sleep(1000)
log(date, "message3")
//迭代器
val it = Iterator(20, 40, 2, 50, 69, 90)
// println(it.size)
//println(it.length)
// println(it.max)
//println(it.min)
while (it.hasNext) {
println(it.next())
}
try {
val f = new FileReader("input.txt")
} catch {
case ex: FileNotFoundException => {
println("Missing file exception")
}
case ex: IOException => {
println("IO Exception")
}
} finally {
println("Exiting finally...")
}
}
def time() = {
println("Getting time in nano seconds")
// System.nanoTime
System.currentTimeMillis()
}
def delayed(t: => Long) = {
println("In delayed method")
println("Param: " + t)
//调用
t;
}
def printInt(a: Int, b: Int) = {
println("Value of a : " + a);
println("Value of b : " + b);
}
//递归调用
def factorial(n: BigInt): BigInt = {
if (n <= 1)
1
else
n * factorial(n - 1)
}
//可变参数
def printStrings(args: String*) = {
var i: Int = 0;
for (arg <- args) {
println("Arg value[" + i + "] = " + arg);
i = i + 1;
}
}
//默认参数值
def addInt(a: Int = 5, b: Int = 7): Int = {
var sum: Int = 0
sum = a + b
return sum
}
def strcat(s1: String)(s2: String) = {
s1 + s2
}
def log(date: Date, message: String) = {
println(date + "----" + message)
}
}
/**
* Created by Administrator on 2015/10/21.
*/
object TestObj {
class Yiibai(val xc: Int, val yc: Int) {
var x: Int = xc
var y: Int = yc
def move(dx: Int, dy: Int) {
x = x + dx
y = y + dy
println("Yiibai x location : " + x);
println("Yiibai y location : " + y);
}
// override def toString(): String="x="+x+",y="+y
override def toString(): String = {
"x=" + x + ",y=" + y
}
}
class Location(override val xc: Int, override val yc: Int,
val zc :Int) extends Yiibai(xc, yc){
var z: Int = zc
def move(dx: Int, dy: Int, dz: Int) {
x = x + dx
y = y + dy
z = z + dz
println ("Yiibai x location : " + x);
println ("Yiibai y location : " + y);
println ("Yiibai z location : " + z);
}
override def toString(): String = {
"x=" + x + ",y=" + y+",z="+z
}
}
def main(args: Array[String]) {
val pp = new Yiibai(10, 20);
pp.move(20, 20);
println(pp)
val loc = new Location(10, 20, 15);
// Move to a new location
loc.move(10, 10, 5);
println(loc)
printYiibai
def printYiibai{
println ("Yiibai x location : " + pp.x);
println ("Yiibai y location : " + pp.y);
}
//http://www.yiibai.com/scala/scala_pattern_matching.html
println(matchTest("two"))
println(matchTest("test"))
println(matchTest(1))
print("Please enter your input : " )
val line = Console.readLine
println("Thanks, you just typed: " + line)
}
//模式匹配
def matchTest(x: Any): Any = x match {
case 1 => "one"
case "two" => 2
case y: Int => "scala.Int"
case _ => "many"
}
}
实例源码下载(右键保存图片,重命名为rar即可):