Skip to content

Instantly share code, notes, and snippets.

@ababup1192
Created May 17, 2019 11:20
Show Gist options
  • Save ababup1192/0615cb59a41babfe2cfed2d35452dccc to your computer and use it in GitHub Desktop.
Save ababup1192/0615cb59a41babfe2cfed2d35452dccc to your computer and use it in GitHub Desktop.
// privateコンストラクタ
class Foo private(text: String) {
// コンパニオンオブジェクトからは、privateな値(関数)にアクセスできる
def foooo: String = text * Foo.constantValue
}
object Foo {
private val constantValue = 2
// コンパニオンオブジェクトからは、privateコンストラクタにアクセスできる
def apply(text: String): Foo = new Foo(text.trim)
}
object Main extends App {
// class Fooのコンストラクタはprivateのためインスタンス化出来ない
// new Foo(" a ")
val foo = Foo.apply(" a ")
// privateな値にはアクセスできない
// Foo.constantValue
println(foo.foooo)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment