Created
May 17, 2019 11:20
-
-
Save ababup1192/0615cb59a41babfe2cfed2d35452dccc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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