Kotlin
Kotlin_Map / Class
λμΊλ¦¬π±
2021. 7. 1. 14:29
λ°μν
Map
/**
*
* λ°λ³΅λ¬Έ https://androidtest.tistory.com/98
*
* Map - ν€μ μμΌλ‘ λ°μ΄ν°κ° μ μ₯λλ©°, μ μλ₯Ό μ¬μ©ν μΈλ±μ€ κΈ°λ° μ²λ¦¬ λμ ν€λ₯Ό κΈ°λ°μΌλ‘ λ°μ΄ν°λ₯Ό μ²λ¦¬νλ€.
* MutableMapOr , mapOf ν¨μλ₯Ό μ¬μ©ν΄μ μμ±λ¨.
* ν€μ 벨λ₯λ₯Ό μ μνκΈ° μν΄ toλ₯Ό μ¬μ©νκ³ μμ.
* toλ ν€μλ μ²λΌ 보μ΄λλ° λ΄λΆμ μΌλ‘λ μ»΄νμΌλ¬κ° "carrie".to(100) μ΄λ κ² λ³ννλ€. - μ μ΄μ μμ±μμ μ΄λ κ² ν΄λ λ¨.
* Pair νμ
μ μ¬μ©ν μλ μμ
*
* λκ°μ keyμ κ°μ μΆκ°νλ €κ³ νλ©΄ (μΆκ°λ +=) ν΄λΉ ν€μ 벨λ₯λ§ λ°λλ€.
*
* ν€ κ°μΌλ‘ κ° κ°μ Έμ¬ μ μλ€.
*
* Mapμμ κ°μ κ°μ Έμ€λ ν¨μ
* [] μΈλ±μ€ μ°μ°μ / getValue / getOrElse / getOrDefault
*
*
* λ³κ²½ κ°λ₯ν Map μ λ³κ²½μ ν¨μ
* [] μΈλ±μ€ μ°μ°μ / += / put / putAll / getOrPut / remove / - / -= / clear
* */
fun main() {
// μ½κΈ° μ μ© map μμ±νκΈ°
val customerGold = mapOf("carrie" to 99.999, "elli" to 99.877, "gom" to 99.888)
val customerGold_1 = mapOf("carrie".to(100), "elli".to(99), "gom".to(100))
val customerGold_2 = mapOf(Pair("carrie", 100), Pair("elli", 99), Pair("gom",100))
println(customerGold)
println(customerGold_1)
println(customerGold_2)
// κ° κ°μ Έμ€κΈ°
println(customerGold["carrie"])
customerGold.forEach{ patron, balance ->
println("${patron}, balance: ${"%.2f".format(balance)}")
}
}
Class
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/trim-indent.html
https://kotlinlang.org/docs/properties.html#getters-and-setters
public/ private/ protected / internal
/**
* ν΄λμ€ Class
* 1. μ½νλ¦°μμλ νλ λλ μ¬λ¬ κ°μ ν΄λμ€λ₯Ό μ μν μ μλ€.
* μ± κ·λͺ¨ 컀μ§μ λ°λ₯Έ μ μ§ λ³΄μ μ©μ΄μ±μ κ³ λ €νμ¬ κ°κΈμ μ΄λ©΄ ν ν΄λμ€μ νλλ₯Ό λ§λλ κ² μ’λ€.
* {} μ¬μ©νμ¬ ν΄λμ€ λͺΈμ²΄λ₯Ό μ μνλ€.
*
* companion Objectλ₯Ό μ¬μ©νλ©΄ μ΄κΈ°ν μμ΄ ν΄λμ€λ₯Ό μ¬μ©ν μ μμ - μ μΌν λ°©λ²μ!
* μ¦, ν΄λμ€λ₯Ό μ¬μ©νλ λ°©λ²μΌλ‘λ
* 1. μ΄κΈ°ν
* 2. companion objectλ‘ λ§λ€κΈ°
*
* μμμ μ¬μ©νλ μ΄μ : κΈ°μ‘΄ μ½λ μ¬μ¬μ©νκΈ° μν¨. → μ½λλ₯Ό μ¬νμ©νλλ°, 체κ³μ μΌλ‘ κ³μΈ΅κ΅¬μ‘°ν ν΄μ μ¬μ©νκΈ° μν΄μ
*
* μ€λ²λΌμ΄λ Parent: open Child: override
* - λμΌν νλ‘νΌν°λ λ©μλλ₯Ό μ¬μ μ ν λ νμν ν€μλ
*
* MainActivityλ₯Ό 보면 AppCompatActivity λ₯Ό μμ λ°λλ°, μ΄λ₯Ό ν΅ν΄ start~ μ΄λ¬ν ν¨μλ€μ μ¬μ©ν μ μλ€.
*
*
* μ€λ²λ‘λ - μμκ΄κ³λμ μκ΄ μμ.
μ */
// λ³μμ ν¨μμ λͺ¨μ
class ν΄λμ€λͺ
{
// μ΄κΈ°ν
init {
// ν΄λμ€λ₯Ό μ΄κΈ°ν νλ©΄ νΈμΆλλ€.
// ν΄λμ€ μ΄κΈ°ν == λ©λͺ¨λ¦¬μ λ‘λ νλ€.
// μ΄ ν΄λμ€ μΈμ€ν΄μ€κ° λλ€.
}
var variagle:String ="" // λ³μ - νλ‘νΌν°
// ν΄λμ€ scope
fun function() // ν¨μ - λ©μλ
{
var variable_local = ""
}
}
//class Log{
// fun d(param1:String, param2:String){
// println("[$param1] : $param2")
// }
//}
class Log{
companion object{
fun d(param1:String, param2:String){
println("[$param1] : $param2")
}
}
}
class Happiness{
companion object{
var H = "happiness"
fun laugh(){
print("haha!")
}
}
}
// λλ μμμ ν μ μλ€ λΌλ μλ―Έλ‘ μμ open ν€μλλ₯Ό λΆμ¬μΌ νλ€.
open class Parent{
var money = 10000
// μμνν
μ€λ²λΌμ΄λκ° λλ €λ©΄ νλ‘νΌν°μ λ©μλ μμμ open μ΄ λΆμ΄μΌ λλ€.
open var house = "μ²μ£Ό 100ν μννΈ"
open fun showHouse()
{
Log.d("Parent","house ${house}")
}
}
// μμ λ°κΈ° - μμ λ°μ κ°μ μ¬μ©νλ €λ©΄ μ΄κΈ°ν ν΄μΌ νλ€.
class Child : Parent(){
override var house = "μ²μ£Ό 10ν μ€νΌμ€ν
"
fun showMoney(){
// μμλ°μΌλ©΄ κ·Έλλ‘ Parent ν΄λμ€μ νλ‘νΌν°μ λ©μλλ₯Ό λ΄κ²μ²λΌ μ¬μ©ν μ μλ€.
Log.d("Child","money ${money}")
}
override fun showHouse()
{
Log.d("Child","house ${house}")
}
}
// μ€λ²λ‘λ
class Son{
fun getNumber():Int {
return 1
}
fun getNumber(param: String) : Int {
return 2
}
}
fun main() {
val name = "carrie"
val healthPoints = 100
val isBlessed = true
val isImmortal = false
val player = Player()
// ν΄λμ€μ μ¬μ© - init ν¨μ νΈμΆ
var cls = ν΄λμ€λͺ
()
cls.variagle
cls.function()
// companion Object μλ λ
// var log = Log()
// log.d("carrie","log")
Happiness.laugh()
// child μ΄κΈ°ν
var child = Child()
child.showMoney()
//override μ¬μ©
var parent = Parent()
var over_child = Child()
parent.showHouse()
over_child.showHouse()
}
var / val - setterμ μ 무
/**
* μμ±μ κ²ν°μ μΈν°.
* : μ°λ¦¬κ° μ μν κ° μμ±μ λν΄ νλμ κ²ν°κ° μλ μμ±λλ©°, κ²½μ°μ λ°λΌ μΈν°λ μλ μμ±λλ€.
* νλ - μμ±μ λ°μ΄ν°κ° μ μ₯λλ κ³³.
*
* κ²ν°μ μΈν° μ€λ²λΌμ΄λ©
* λ€ν΄νΈλ‘ μλλ° getter, setterλ₯Ό 컀μ€ν
ν μ μλ€.
* λν΄νΈ : μ½νλ¦°μμλ νλλͺ
μμ²΄κ° κ²ν° μΈν° μν μ ν¨
* 컀μ€ν
: νλ field
*
*
* constructor, init λ λ€ μ΄κΈ°ν μΈλ°, μ€νμ init → constructor μμλ‘ λλ€.
*
* trimIndent{}
*
*
* κΈ°λ³Έ default μ£Όμ μμ±μ
* class Student
* class Student{}
* class Student() {}
*
*
*
*
* * */
fun main() {
val carrie = Student("carrie", 24)
val elli = Student(null, 24, 2.0f)
val john = Student("john", 20,1.0f)
// carrie.realScore = 3.3f
carrie.showStatus()
elli.showStatus()
john.showStatus()
}
// μ£Όμμμ±μ μΈμλ₯Ό μ΄μ©ν΄μ νλ μ΄κΈ°ν
//class Student(name: String?, age:Int, score:Float = 1.0f)
//{
// val realName: String = name ?:"guest"
// val email: String = "${realName}@school.org"
// val joinedAge: Int = age
// var realScore: Float = score
//
// fun showStatus()
// {
// val status = """
// === νμ μ¬ν μ¦λͺ
===
// μ΄λ¦: ${realName}
// λμ΄: ${joinedAge}
// νμ : ${realScore}
// μ΄λ©μΌ: ${email}
// ======================
// """.trimIndent()
//
// println(status)
// }
//
//}
// μΈμμΈ λμμ νλν
class Student(val name: String?,val age:Int, var score:Float = 1.0f)
{
val email: String = "${name}@school.org"
// var μ 컀μ€ν
getter, setter
var description: String? = null
get() = field ?: "null μ΄κ΅¬λ"
set(value){
if(!value.isNullOrEmpty())
field = value
}
fun showStatus()
{
val status = """
=== νμ μ¬ν μ¦λͺ
===
μ΄λ¦: ${name}
λμ΄: ${age}
νμ : ${this.score}
μ΄λ©μΌ: ${email}
μ€λͺ
: ${description}
======================
""".trimIndent()
println(status)
}
}
late lazy
https://www.youtube.com/watch?v=I-TksKaou1Q
μ»΄νμΌλ¬λ μ΄κΈ°ν λΈλ‘μμ μμ±μ μ¬μ©νλ ν¨μμ λΉκ΅νλ©΄μκΉμ§ μμ±μ μ΄κΈ°ν μμλ₯Ό κ²μ¬νμ§λ μλλ€. λ°λΌμ μμμ μ¬μ©νλ ν¨μλ₯Ό νΈμΆνλ μ΄κΈ°ν λΈλ‘μ μ μν λλ ν΄λΉ ν¨μλ₯Ό νΈμΆνκΈ° μ μ κ·Έλ° μμμ΄ μ΄κΈ°νλλλ‘ μ½λλ₯Ό μμ±ν΄μΌνλ€.
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
/**
* lateinit : λ¦μ μ΄κΈ°ν
* by lazy : κ²μΌλ₯Έ μ΄κΈ°ν(νλΌνΌν° μμ μ΄κΈ°ν)
* Val κ°μ κ²½μ°λ λ¨ νλ²μ μ΄κΈ°νλ§ νλκΉ,
*
* lateinit
* >> μ§κΈ λΉμ₯ null λ‘ λκ³ μΆμ§ μμλ°, λν΄νΈλ‘ λ€λ₯Έ κΈ°λ³Έ μ μΈ κ²μ νκΈ°μλ μ‘°κΈ κΉλ€λ‘λ€. κ·Έλμ λ±μ₯ν κ²μ΄ λ°λ‘ lateinit!!
* >> μλμ μΌλ‘ λ³μ νλ‘νΌν°μ μ΄κΈ°νλ₯Ό μ§μ°μν€λ κ²
*
* init block :
* μ£Όμ μμ±μμ value - parameterλ νλ¬νΌν° νλΌλ―Έν°λ₯Ό μ¬μ©νμ¬ μ΄κΈ°ν / init { μ΄κΈ°ν μ½λ }
*
* secondary constructors :
* 2μ°¨ μμ±μλ₯Ό ν κ° μ΄μ λ μ μν μ μμ / μ£Όμ μμ±μ, λ€λ₯Έ 2μ°¨ μμ±μλ₯Ό νΈμΆνμ¬ μ΄κΈ°νλ₯Ό μμν μ μμ
*
*
* */
enum class Monster {
WaterMon, FireMon, EarthMon, WindMon
}
enum class Clan{
DarkSide, LightSide
}
class Player(){
// lateinit - μ§κΈ μ°λ¦¬κ° nullμ΄λ κΈ°λ³Έ κ°μ΄λ μ무 κ²λ μ΄κΈ°ν μν΄λ error λ΄μ§ μκ² λ€! λμ€μ μ λ§λ‘ κ° λΆμ¬ λ ν
λ° κ·ΈλλΆν° μ°κ² λ€.
lateinit var nick: String // = ""
// init {
//
// }
lateinit var leadMonster:Monster // = Monster.EarthMon
// μ§μ
// μ΄κΈ°νλ₯Ό λ¨ νλ² νμλ‘ νλ μμ μ κ·Έλ λ± νκ² λ€. by lazy!
val clan:Clan by lazy{
// Clanμ κ²°μ μ΄κΈ°ν μ½λ λΈλ‘.
this.selectClan()
} // = null // Clan.DarkSide
// μ§μ κ³ λ₯΄λ ν¨μ
private fun selectClan(): Clan {
var mySide: Clan = Clan.DarkSide
try {
// Clan μ΄λΌκ³ νλ μ΄κ±°ν ν΄λμ€μλ, staticμ λ²κΈκ°λ ν¨μλ€μ κ°μ§κ³ μμ.
Clan.values().forEach {
print("${it.ordinal}: ${it.name} μ§μ \t")
}
print("Player μ§μμ μ ν : ")
// κ°ν λ¬Έμλ₯Ό λ²νΌμ λ£μ΄ λμλ€κ°, νλμ λ¬Έμμ΄λ‘ νλμ νμ λμ Έμ£Όλ ν¨μ
val br: BufferedReader = BufferedReader(
InputStreamReader(System.`in`)
)
val sel = br.readLine()
if (sel == "1") mySide = Clan.LightSide
}catch (e: IOException){
e.printStackTrace()
}
return mySide
}
}
class Professor(_name: String, _age:Int = 30){
var name: String? = null
val age: Int = _age
init{
// μ£Όμμμ±μμ μ΄κΈ°ν λΈλ‘
// init λΈλ‘ μμ μλ νλ‘νΌν° μ¬μ© κ°λ₯ init λΈλ‘ λ€μ μμ΄λ μ€νμ λμ§λ§ μ£Όμκ° νμν¨.
name = if(_name.isNotEmpty() && _name.isNotBlank()) _name.toUpperCase() else "Guest"
println("$name κ΅μλ ($age) μΈμ€ν΄μ€")
}
var major: String = "μΈλ¬Έν"
// κ΄νΈ μμλ νλ‘νΌν°κ° μλ 벨λ₯ κ°λ§ λ€μ΄κ° μ μμ
constructor(_name : String, _age: Int, _major: String)
: this(_name, _age)
{
// this.name = _name
// this.age = _age
this.major = _major
}
}
fun main() {
val p1 = Professor("david", 39) // p.c.
val p2 = Professor("alice") // p.c.
val p3 = Professor("park",45,"κ²½μ ν") // 2μ°¨ μμ±μ
}
λ°μν