Scala 基礎


object・・・シングルトンなクラス
class・・・クラス
object Util{
 def print = {
   println("Hello")
 }
}
class Sample{
 def paint={
   Util.print()
 }
}

var・・・変数=可変
val・・・値=不可変
object SampleApp {
  def main(args : Array[String]) : Unit = {
    val a1 = new Test1("test1");
    val a2 = new Test2("test2");
    //a1.a = "test3";//error 
    a2.a = "test4";
    val a3 = new Test3();
    var i = a3.a1;
    //a3.a1 = i;//error
    i = a3.b1;
    i = i+1
    a3.b1 = i;
  }
}
class Test1(val a: String){
}
class Test2(var a: String){ 
}
class Test3(){
  val a1 = 1;
  var b1 = 2;
}

def 関数名(引数:引数型):戻り値型={} ・・・関数
abstract class Method1 {
 def method;
}
class Method2 {
 def method = {};
}

class Point(var x : Int,var y : Int, var z: Int){
 def -(p :Point) : Point = { new Point(x -p.x , y -p.y , z -p.z )}
 def +(p :Point) : Point = { new Point(x +p.x , y +p.y , z +p.z )}
 override def toString = { "[" + x + ","+ y + "," + z +"]"}
}

HelloWorld
object FirstApp {
  def main(args : Array[String]) : Unit = {
    Console.println("hello")
  }
}

Application
object AppSample extends Application{
  println("test");
}

タプル tuple
 def test = {
   val tuple = ("tap", "le", 10);
   println(tuple._1);
   println(tuple._2);
   println(tuple._3);
 }
最終更新:2009年03月17日 23:48
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。