Created
July 10, 2019 05:50
-
-
Save herisulistiyanto/48878ac7832119e3edadb54bd27d808e to your computer and use it in GitHub Desktop.
OOP Playground
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
fun main() { | |
val shotGun = ShotGun("S12K", "Brown").apply { | |
projectile = 3 | |
} | |
val deagle = Deagle("Deagle", "Red").apply { | |
projectile = 3 | |
} | |
val snipper = Snipper().apply { | |
selectedGun = shotGun | |
} | |
val info1 = snipper.selectedGun?.type.orEmpty() | |
val info2 = snipper.selectedGun?.color.orEmpty() | |
println(info1) | |
println(info2) | |
repeat(3) { | |
snipper.shoot() | |
} | |
println("\n================================= next gun >>>>>\n") | |
snipper.selectedGun = deagle | |
val info3 = snipper.selectedGun?.type.orEmpty() | |
val info4 = snipper.selectedGun?.color.orEmpty() | |
println(info3) | |
println(info4) | |
repeat(3) { | |
snipper.shoot() | |
} | |
} | |
abstract class Gun(val type: String, val color: String) : TheGunListener { | |
internal var projectile: Int = 0 | |
internal fun showStatusShot(projectileRemain: Int) { | |
if (projectileRemain > 0) { | |
println("Completed. The projectile was $projectileRemain") | |
} else { | |
println("Failed. Please reload") | |
} | |
} | |
} | |
class ShotGun(curType: String, curColor: String) : Gun(curType, curColor) { | |
override fun onShotListener() { | |
this.projectile-- | |
showStatusShot(this.projectile) | |
} | |
} | |
class Deagle(curType: String, curColor: String) : Gun(curType, curColor) { | |
override fun onShotListener() { | |
this.projectile -= 2 | |
showStatusShot(this.projectile) | |
} | |
} | |
class Snipper { | |
var selectedGun: Gun? = null | |
fun shoot() { | |
this.selectedGun?.onShotListener() | |
} | |
} | |
interface TheGunListener { | |
fun onShotListener() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment