ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

val ๊ทธ๋ฆฌ๊ณ  var

getter, setter๋ฅผ ์ด์•ผ๊ธฐํ•˜๋Š”๋ฐ ์›ฌ val var ์˜ ์ด์•ผ๊ธฐ๋ƒ ์‹ถ์„ ์ˆ˜ ์žˆ์ง€๋งŒ ์ œ๋Œ€๋กœ ๊ณต๋ถ€ํ•ด๋ณด๋‹ˆ ์ด ์ด์•ผ๊ธฐ๋ฅผ ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์—†๋‹ค.

val, var์˜ ์˜๋ฏธ

val : value

var : variable


 

์ •๋ง ๊ธฐ๋ณธ ์ ์ธ ์‚ฌ์‹ค ์ด์ง€๋งŒ ๊ฐ„๊ณผํ•˜๊ณ  ์žˆ์—ˆ๋‹ค. ๊ทธ๋™์•ˆ์€ ์•„๋ž˜์™€ ๊ฐ™์ด ์•Œ๊ณ  ์žˆ์—ˆ์ง€๋งŒ ์‚ฌ์‹ค ์ด ๋‚ด์šฉ์œผ๋กœ๋งŒ ์•Œ๊ณ  ์žˆ์—ˆ๋‹ค๋ฉด ๋น„์•ฝ์ด๋‹ค.

val : immutable 
var : mutable 

 

getter setter์˜ ๊ฐœ๋…๋„ ์บก์Šํ™”์™€ ๊ด€๋ จํ•œ ๊ธฐ๋ณธ์ ์ธ ๊ฐœ๋…์ธ๋ฐ, ๊ทธ๋ณด๋‹ค ๋” ๊ธฐ๋ณธ์ ์ธ ๊ฐœ๋…์˜ val var ์ด์•ผ๊ธฐ๋ฅผ ์™œ ํ•˜๋Š๋ƒ?

 

 

Kotlin์€ ๋ณ€์ˆ˜์˜ getter(), setter()๋ฅผ ๋‚ด๋ถ€์ ์œผ๋กœ ์ƒ์„ฑํ•ด์ฃผ๋Š” ์žฅ์ ์ด ์žˆ๋‹ค. ์ด์™€ ๊ด€๋ จํ•˜์—ฌ val, var์˜ ํ•ต์‹ฌ์ ์ธ ์ฐจ์ด๋Š” ๋ฐ”๋กœ ์ด๊ฒƒ์ด๋‹ค.

val : getter ๋งŒ ์ œ๊ณต, var : getter์™€ setter ๋ชจ๋‘ ์ œ๊ณต

 

val ์ž์ฒด๊ฐ€ immutable ํ•˜๊ณ  var ์ž์ฒด๊ฐ€ mutable ํ•œ ๊ฒƒ์ด๋ผ๊ธฐ ๋ณด๋‹ค๋Š” setter๊ฐ€ ์ œ๊ณต๋˜๋ƒ, ๋˜์ง€ ์•Š๋ƒ์˜ ์ฐจ์ด์—์„œ ๋ฐœ์ƒํ•œ๋‹ค.

์ด๋ ‡๊ฒŒ ๋•Œ๋ฌธ์— val๋กœ ์ •์˜๋œ ๋ณ€์ˆ˜๋ฅผ var๋กœ override ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค. 

interface Game {
    val name: String
}

class NightCrow(override var name: String) : Game
// Game interface์— val๋กœ ์ •์˜๋œ name ๋ณ€์ˆ˜๋ฅผ var๋กœ override ํ•  ์ˆ˜ ์žˆ๋‹ค

 


 

Getter ๊ทธ๋ฆฌ๊ณ  Setter

์•ž์„œ ๋งํ–ˆ ๋“ฏ Kotlin์€ getter()์™€ setter()๋ฅผ ๋‚ด๋ถ€์ ์œผ๋กœ ์ƒ์„ฑํ•œ๋‹ค. ์•„๋ž˜ Kotlin ์†Œ์Šค๋ฅผ Java๋กœ Decompileํ•œ ๊ฒฐ๊ณผ์™€ ๋น„๊ตํ•ด๋ณด์ž.

 

 Kotlin 

class GetterSetterTestModel() {
    var firstName: String = "HeeDong"
    val lastName: String = "Gang"

    val fullName: String
        get() = firstName + lastName

    val fullName1: String = firstName + lastName
}

 

 Java 

public final class GetterSetterTestModel {
   @NotNull
   private String firstName = "HeeDong";
   @NotNull
   private final String lastName = "Gang";
   @NotNull
   private final String fullName1;

   @NotNull
   public final String getFirstName() {
      return this.firstName;
   }

   public final void setFirstName(@NotNull String var1) {
      this.firstName = var1;
   }

   @NotNull
   public final String getLastName() {
      return this.lastName;
   }

   @NotNull
   public final String getFullName() {
      return this.firstName + this.lastName;
   }

   @NotNull
   public final String getFullName1() {
      return this.fullName1;
   }

   public GetterSetterTestModel() {
      this.fullName1 = this.firstName + this.lastName;
   }
}

 

 ๋น„๊ต ๊ฒฐ๊ณผ - val, var์˜ ์ฐจ์ด 

  Kotlin java
1 var firstName = "HeeDong" private String firstName = "HeeDong"

  public final String getFirstName() {
      return this.firstName;}

public final void setFirstName(@NotNull String var1) {
      this.firstName = var1;}
2 val lastName = "Gang" private final String lastName = "Gang";

public final String getLastName() {
      return this.lastName;}
  1. Kotlin์˜ var firstName๋Š” Java๋กœ decompile์‹œ final String getFirstName() ํ•จ์ˆ˜์™€
    public final void setFirstName()์˜ getter setter๊ฐ€ ์ƒ์„ฑ๋จ
  2. val lastName = "Gang"์€ public final String getLastName() getter๊ฐ€ ์ƒ์„ฑ๋จ

 

 ๋น„๊ต ๊ฒฐ๊ณผ - ๊ธฐ๋ณธ getter์™€ custom getter์˜ ์ฐจ์ด 

  Kotlin java
1     val fullName: String
        get() = firstName + lastName
public final String getFullName() {
      return this.firstName + this.lastName;}
2 val fullName1: String = firstName + lastName public GetterSetterTestModel() {
      this.fullName1 = this.firstName + this.lastName;}

   public final String getFullName() {
      return this.firstName + this.lastName;}
  1. val fullName ๊ณผ ๊ฐ™์€ custom getter๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ ์ฐธ๊ณ ํ•˜๊ณ  ์žˆ๋Š” firstName, lastName๋ณ€์ˆ˜์˜ ํ˜„์žฌ ๊ฐ’์„ ๊ทธ๋Œ€๋กœ returnํ•จ
  2. val fullName1๊ณผ ๊ฐ™์ด ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•œ ๊ฒฝ์šฐ GetterSetterTestModel ์ƒ์„ฑ์ž์—์„œ ์ดˆ๊ธฐ๊ฐ’์„ ์„ค์ •ํ•˜๊ณ , ์ฐธ์กฐํ•˜๊ณ  ์žˆ๋Š” ๋ณ€์ˆ˜์˜ ์ดˆ๊ธฐ๊ฐ’๋งŒ์„ ์ฐธ๊ณ ํ•˜์—ฌ ๊ฐ’์„ assignํ•จ