程序主入口类,基本覆盖了入门需要掌握的Scala知识。主要涵盖,变量定义,常量,类与对象,集合映射等常用方法。
package org.lean.scala
import org.learn.clazz.Person
import scala.collection.immutable.HashMap
import scala.io.Source
import java.io.PrintWriter
import java.io.File
object Main {
private var myName: String = "";
def main(args: Array[String]) {
println("HelloWorld");
var byte_tmp: Byte = 255.toByte
var short_tmp: Short = 255.toShort;
var int_tmp: Int = 255;
var long_tmp: Long = 255.toLong;
var dbl_tmp: Double = 255.toDouble;
var flt_tmp: Float = 255.toFloat;
var str_tmp: String = 255.toString();
var bool_tmp: Boolean = 255.!=(0);
println(str_tmp.equals(null));
var anysTMP = 12; //表示变量
val anysTMPl = "Good Baby"; //表示常量
anysTMP.+=(24); //闭包式 同 anysTMP=anysTMP+24;
println(anysTMP);
//anysTMPl="Hello"; error,not support
println(anysTMPl + " HELLO");
//println(AnotherObj.myName); error,not support
println(AnotherObj.myAge);
//Class and Object
var p = new Person
p.setName("Hword");
print(p.getName);
//Array
var arTmp = new Array[String](3);
arTmp(0) = "Good World";
arTmp(1) = "I'm Jack";
arTmp(2) = "boast show";
for (i <- 0 to 2) {
println(arTmp(i))
}
//List
var listTmp = List(1, 2, 3);
println(listTmp(0));
listTmp = 90 :: listTmp; //concat element,but not support different Type of Element
println(listTmp(0));
println(listTmp);
//useful method of List
var countl = listTmp.count { x => x > 3 }
println(countl)
//drop
println(listTmp.drop(1));
println(listTmp.dropRight(1));
//exist check
println(listTmp.exists { x => x == 90 })
//filter
println(listTmp.filter { x => x > 1 })
//loop for all element,return boolearn result
println(listTmp.forall { x => x > 0 })
//loop for all with any method
listTmp.foreach { x => println(" this is list's foreach =: " + x) }
//map ,return self by changes
println(listTmp.map { x => x * x })
//build string with any string
println(listTmp.mkString("#"))
//reverse
println(listTmp.reverse);
//sort
println(listTmp.sortWith({ (c: Int, n: Int) => c > n }))
//Tuple Section
val tplTmp = ("Google", "Baidu", 0x9002)
println(tplTmp);
println(tplTmp._1); // Note: start index is 1
println(tplTmp._2);
println(tplTmp._3);
//Set And Map
var setTmp = Set("hao123", "hao360");
println(setTmp)
println(setTmp + "hao163")
var mapTmp = new HashMap[String, String]();
mapTmp.+=("KEY1" -> "Hao123");
mapTmp.+=("KEY2" -> "Hao321");
println(mapTmp("KEY1"));
println(mapTmp.get("KEY1"));
//File's read and write
for (line <- Source.fromFile("d.txt").getLines()) {
println(line)
}
var writer:PrintWriter=new PrintWriter(new File("d.txt"));
writer.write("Im Test Line");
writer.close()
//singleton
singleObj.name="Hello Scala";
println(singleObj.name);
}
object singleObj{
var name:String="";
}
}
package org.learn.clazz
class Person {
private var rName: String = "";
private var age: Int = 255;
def getName(): String = {
return rName;
}
def setName(name:String)={
rName=name;
}
}
package org.lean.scala
object AnotherObj {
private var myName:String="Another";
var myAge:Int=12;
}
另外两个文件一个是单态对象定义,一个是Class定义

被折叠的 条评论
为什么被折叠?



