Created
January 6, 2018 06:05
-
-
Save YusukeIwaki/dc3acad41c18a927c8b2d1de40319707 to your computer and use it in GitHub Desktop.
backing fieldのgetでCloseしないといけないリソースを返して、そいつをuseしたときにitで新たなリソースが生成されないことを確認する
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
import java.io.Closeable | |
class Resource: Closeable { | |
init { | |
println("resource ref +1") | |
} | |
fun play() { | |
println("play") | |
} | |
override fun close() { | |
println("resource ref -1") | |
} | |
} | |
class Practice { | |
private val resource | |
get() = Resource() | |
fun execute() { | |
resource.use { | |
it.play() | |
} | |
resource.use { | |
resource.use { | |
it.play() | |
it.play() | |
} | |
resource.use { | |
it.play() | |
it.play() | |
it.play() | |
} | |
} | |
} | |
} | |
fun main(args: Array<String>) { | |
Practice().execute() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
実行結果