ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Kotlin] Companion Object λž€?
    Android/Kotlin 2021. 7. 5. 23:13
    λ°˜μ‘ν˜•
    • 이 글은 "μœ€μž¬μ„±μ˜ Google 곡식 μ–Έμ–΄ kotlin" κ°•μ˜λ₯Ό λ“£κ³  κ³΅λΆ€ν•œ λ‚΄μš©μ„ μž‘μ„±ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

    Java Static

    • 클래슀 λ‚΄λΆ€μ˜ 멀버 λ³€μˆ˜λ₯Ό μ‚¬μš©ν•˜κΈ° μœ„ν•΄μ„œλŠ” 클래슀λ₯Ό μƒμ„±ν•˜κ³  μΈμŠ€ν„΄μŠ€.λ³€μˆ˜μ˜ ν˜•νƒœλ‘œ ν˜ΈμΆœν•˜μ—¬ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
    • μ—¬λŸ¬ 개의 μΈμŠ€ν„΄μŠ€λ₯Ό λ§Œλ“€ μ‹œ 각각 멀버 λ³€μˆ˜κ°€ μ €μž₯될 λ©”λͺ¨λ¦¬λ₯Ό μ‚¬μš©ν•˜κ²Œ λ©λ‹ˆλ‹€.
    • ν•˜μ§€λ§Œ, λͺ¨λ“  μΈμŠ€ν„΄μŠ€μ—μ„œ κ³΅ν†΅μ μœΌλ‘œ μ‚¬μš©ν•˜λŠ” λ³€μˆ˜κ°€ μžˆμ„ κ²½μš°μ—λŠ” μ—¬λŸ¬ 개의 λ©”λͺ¨λ¦¬λ₯Ό μ‚¬μš©ν•˜λŠ” 것은 λΉ„νš¨μœ¨μ μΌ 수 μžˆμŠ΅λ‹ˆλ‹€.
    • λ”°λΌμ„œ 이λ₯Ό ν•΄κ²°ν•˜κΈ° μœ„ν•΄μ„œλŠ” static λ³€μˆ˜, λ©”μ†Œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ ν•΄λ‹Ή 클래슀의 λͺ¨λ“  μΈμŠ€ν„΄μŠ€μ—μ„œλŠ” κ³΅ν†΅μ μœΌλ‘œ μ‚¬μš©ν•˜κ²Œ λ§Œλ“€ 수 μžˆμŠ΅λ‹ˆλ‹€.
    class Student {
        public String id;
        public String name;
        public static String national;
    
        public static void printInfo(){
            System.out.printf("printInfo");
        }
    }
    
    public class Main {
        public static void main(String[] args) {
    
            Student.national = "korea";
            Student.printInfo();
        }
    }

    Static ν‚€μ›Œλ“œκ°€ 뢙인 λ³€μˆ˜ λ˜λŠ” λ©”μ†Œλ“œλŠ” 컴파일 이후 클래슀 λ‘œλ”© μ‹œμ μ—μ„œ ν΄λž˜μŠ€μ™€ 같이 클래슀 λ³€μˆ˜λ„ λ©”λͺ¨λ¦¬λ₯Ό μ°¨μ§€ν•˜κ²Œ λ©λ‹ˆλ‹€. Static λ³€μˆ˜λ‚˜ λ©”μ†Œλ“œλŠ” 클래슀.λ³€μˆ˜ 둜 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

     

    Companion Objectλž€?

    • μžλ°”μ˜ static을 μ—†μ• κ³  kotlinμ—μ„œλŠ” Companion ObjectλΌλŠ” λ™λ°˜ 객체λ₯Ό μ‚¬μš©ν•˜μ—¬ 정적 멀버λ₯Ό μ •μ˜ν•©λ‹ˆλ‹€.
    • λ˜ν•œ, Companion에 이름을 μ„€μ •ν•˜μ—¬ μ»€μŠ€ν…€ν•œ Companion Objectλ₯Ό λ§Œλ“€ 수 μžˆμŠ΅λ‹ˆλ‹€.
    • μ°Έκ³ (https://www.bsidesoft.com/8187)
    class Student{
        var id: String? = null
        var name: String? = null
    
        companion object{
            val national = "korea"
    
            fun printnational(){
                println(national)
            }
        }
    }
    
    fun main(){
        println(Student.Companion.national)
        Student.Companion.printnational()
    
        val comp = Student.Companion
        println(comp.national)
        comp.printnational()
    
        val comp2 = Student
        println(comp2.national)
        comp2.printnational()
    }
    • μžλ°”μ˜ staticκ³ΌλŠ” 쑰금 차이점이 μ‘΄μž¬ν•˜λŠ”λ° Companion Object둜 객체이닀 λ³΄λ‹ˆ λ³€μˆ˜μ— ν• λ‹Ήν•  수 μžˆμŠ΅λ‹ˆλ‹€.
    • ν•˜λ‚˜μ˜ ν΄λž˜μŠ€μ—μ„œλŠ” ν•˜λ‚˜μ˜ Companion Object만 생성할 수 μžˆμŠ΅λ‹ˆλ‹€.
    • Companion Object λ‚΄λΆ€μ˜ λ³€μˆ˜λŠ” 클래슀 μ•ˆμ—μ„œ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ 클래슀 멀버 λ³€μˆ˜λŠ” Companion Object μ•ˆμ—μ„œ μ‚¬μš©ν•  수 μ—†μŠ΅λ‹ˆλ‹€. Companion ObjectλŠ” 클래슀 λ‘œλ”©ν•˜λ©΄μ„œ μƒμ„±λ˜μ§€λ§Œ, 클래슀 λ©€λ²„λ³€μˆ˜λŠ” 클래슀 μΈμŠ€ν„΄μŠ€λ₯Ό 생성할 λ•Œ μƒμ„±λ˜κΈ° λ•Œλ¬Έμ— λ©€λ²„λ³€μˆ˜λ₯Ό μ‚¬μš©ν•  수 μ—†μŠ΅λ‹ˆλ‹€.
    public final class Student {
       @Nullable
       private String id;
       @Nullable
       private String name;
       @NotNull
       private static final String national = "korea";
       public static final Student.Companion Companion = new Student.Companion((DefaultConstructorMarker)null);
    
       @Nullable
       public final String getId() {
          return this.id;
       }
    
       public final void setId(@Nullable String var1) {
          this.id = var1;
       }
    
       @Nullable
       public final String getName() {
          return this.name;
       }
    
       public final void setName(@Nullable String var1) {
          this.name = var1;
       }
    
       public static final class Companion {
          @NotNull
          public final String getNational() {
             return Student.national;
          }
    
          public final void printnational() {
             String var1 = ((Student.Companion)this).getNational();
             boolean var2 = false;
             System.out.println(var1);
          }
    
          private Companion() {
          }
    
          // $FF: synthetic method
          public Companion(DefaultConstructorMarker $constructor_marker) {
             this();
          }
       }
    }

    μžλ°”λ‘œ Decompile해보면 Student 클래슀 내뢀에 static으둜 Companion 클래슀λ₯Ό μƒμ„±ν•˜λŠ” 것을 확인할 수 μžˆμŠ΅λ‹ˆλ‹€. 이λ₯Ό 톡해 Companion κ°μ²΄λŠ” ν΄λž˜μŠ€κ°€ 생성될 λ•Œ λ©”λͺ¨λ¦¬μ— μ μž¬λ˜λ©΄μ„œ λ™μ‹œμ— μƒμ„±ν•˜λŠ” κ°μ²΄λΌλŠ” 것을 μ•Œ 수 μžˆμŠ΅λ‹ˆλ‹€.

    λ°˜μ‘ν˜•
Designed by Tistory.