Skip to content

Instantly share code, notes, and snippets.

@ryokosuge
Last active December 24, 2015 03:29
Show Gist options
  • Save ryokosuge/6737705 to your computer and use it in GitHub Desktop.
Save ryokosuge/6737705 to your computer and use it in GitHub Desktop.
console.scala
// scala.xmlのインポート
scala> import scala.xml._
import scala.xml._
// scala.xml.transformのインポート
scala> import transform._
import transform._
// scala.collection.mutableパッケージのHashMapをインポート
scala> import scala.collection.mutable.HashMap
import scala.collection.mutable.HashMap
// 複数のオブジェクトやクラスのインポート
scala> import scala.collection.immutable.{TreeMap, TreeSet}
import scala.collection.immutable.{TreeMap, TreeSet}
// インポートしたクラスに別の名前をつける
scala> import scala.util.parsing.json.{ JSON => JsonParser }
import scala.util.parsing.json.{JSON=>JsonParser}
scala> JSON
<console>:19: error: not found: value JSON
JSON
^
scala> JsonParser
res2: scala.util.parsing.json.JSON.type = scala.util.parsing.json.JSON$@6a0f0c67
// scalaのクラス定義
scala> class Foo
defined class Foo
// Javaだと
public class Foo {
}
// インスタンス作成
scala> new Foo
res3: Foo = Foo@5f95bffc
scala> new Foo()
res4: Foo = Foo@6a8fcfae
// コンストラクタで1つの引数(String)をとるBarクラスの定義
scala> class Bar(name: String)
defined class Bar
// Barクラスのインスタンス生成
scala> new Bar("Hello World")
res5: Bar = Bar@aad33f6
// コンストラクタでの引数チェック
scala> class Baz(name: String) {
// 初期化処理
if (name == null) throw new Exception("Name is null")
}
defined class Baz
// トレイト
scala> trait Dog
defined trait Dog
// トレイトの追加
scala> class Fizz2(name: String) extends Bar(name) with Dog
defined class Fizz2
// メソッド定義ありのトレイト
scala> trait Cat {
def meow(): String
}
defined trait Cat
scala> trait FuzzCat extends Cat{
override def meow(): String = "Meeeeeow"
}
defined trait FuzzCat
scala> trait OtherThing {
def hello() = 4
}
defined trait OtherThing
scala> class Yep extends FuzzCat with OtherThing
defined class Yep
scala> (new Yep).meow()
res6: String = Meeeeeow
scala> (new Yep).hello()
res7: Int = 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment