2017_10_21_kotlin_study - denlyou/AIL GitHub Wiki

  • DataClass.kt
// data class Human(var name: String, var age: Int, var isMan: Boolean){
data class Human(var name: String, var age: Int){
    // 
}

fun main(args: Array<String>){
    // var h = Human("bin", 33, true)
    var h = Human("bin", 33)
    println(h) 
    // var h2 = Human("bin", 33, false)
    var h2 = Human("bin", 33)
    println(h == h2)
    println(h === h2)
    
    // val (gender, name, age) = h
    val (name, age) = h
    // println("$gender, $name, $age")
    println("$name, $age")
}
  • DataClass.decompile.java
// DataClassKt.java
import kotlin.Metadata;
import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;

@Metadata(
   mv = {1, 1, 7},
   bv = {1, 0, 2},
   k = 2,
   d1 = {"\u0000\u0014\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0010\u0011\n\u0002\u0010\u000e\n\u0002\b\u0002\u001a\u0019\u0010\u0000\u001a\u00020\u00012\f\u0010\u0002\u001a\b\u0012\u0004\u0012\u00020\u00040\u0003¢\u0006\u0002\u0010\u0005¨\u0006\u0006"},
   d2 = {"main", "", "args", "", "", "([Ljava/lang/String;)V", "production sources for module FirstProject"}
)
public final class DataClassKt {
   public static final void main(@NotNull String[] args) {
      Intrinsics.checkParameterIsNotNull(args, "args");
      Human h = new Human("bin", 33);
      System.out.println(h);
      Human h2 = new Human("bin", 33);
      boolean var3 = Intrinsics.areEqual(h, h2);
      System.out.println(var3);
      var3 = h == h2;
      System.out.println(var3);
      String name = h.component1();
      int age = h.component2();
      String var5 = "" + name + ", " + age;
      System.out.println(var5);
   }
}
// Human.java
import kotlin.Metadata;
import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;

@Metadata(
   mv = {1, 1, 7},
   bv = {1, 0, 2},
   k = 1,
   d1 = {"\u0000 \n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0010\u000e\n\u0000\n\u0002\u0010\b\n\u0002\b\r\n\u0002\u0010\u000b\n\u0002\b\u0004\b\u0086\b\u0018\u00002\u00020\u0001B\u0015\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u0012\u0006\u0010\u0004\u001a\u00020\u0005¢\u0006\u0002\u0010\u0006J\t\u0010\u000f\u001a\u00020\u0003\u0003J\t\u0010\u0010\u001a\u00020\u0005\u0003J\u001d\u0010\u0011\u001a\u00020\u00002\b\b\u0002\u0010\u0002\u001a\u00020\u00032\b\b\u0002\u0010\u0004\u001a\u00020\u0005\u0001J\u0013\u0010\u0012\u001a\u00020\u00132\b\u0010\u0014\u001a\u0004\u0018\u00010\u0001\u0003J\t\u0010\u0015\u001a\u00020\u0005\u0001J\t\u0010\u0016\u001a\u00020\u0003\u0001R\u001a\u0010\u0004\u001a\u00020\u0005X\u0086\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b\u0007\u0010\b\"\u0004\b\t\u0010\nR\u001a\u0010\u0002\u001a\u00020\u0003X\u0086\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b\u000b\u0010\f\"\u0004\b\r\u0010\u000e¨\u0006\u0017"},
   d2 = {"LHuman;", "", "name", "", "age", "", "(Ljava/lang/String;I)V", "getAge", "()I", "setAge", "(I)V", "getName", "()Ljava/lang/String;", "setName", "(Ljava/lang/String;)V", "component1", "component2", "copy", "equals", "", "other", "hashCode", "toString", "production sources for module FirstProject"}
)
public final class Human {
   @NotNull
   private String name;
   private int age;

   @NotNull
   public final String getName() {
      return this.name;
   }

   public final void setName(@NotNull String var1) {
      Intrinsics.checkParameterIsNotNull(var1, "<set-?>");
      this.name = var1;
   }

   public final int getAge() {
      return this.age;
   }

   public final void setAge(int var1) {
      this.age = var1;
   }

   public Human(@NotNull String name, int age) {
      Intrinsics.checkParameterIsNotNull(name, "name");
      super();
      this.name = name;
      this.age = age;
   }

   @NotNull
   public final String component1() {
      return this.name;
   }

   public final int component2() {
      return this.age;
   }

   @NotNull
   public final Human copy(@NotNull String name, int age) {
      Intrinsics.checkParameterIsNotNull(name, "name");
      return new Human(name, age);
   }

   // $FF: synthetic method
   // $FF: bridge method
   @NotNull
   public static Human copy$default(Human var0, String var1, int var2, int var3, Object var4) {
      if ((var3 & 1) != 0) {
         var1 = var0.name;
      }

      if ((var3 & 2) != 0) {
         var2 = var0.age;
      }

      return var0.copy(var1, var2);
   }

   public String toString() {
      return "Human(name=" + this.name + ", age=" + this.age + ")";
   }

   public int hashCode() {
      return (this.name != null ? this.name.hashCode() : 0) * 31 + this.age;
   }

   public boolean equals(Object var1) {
      if (this != var1) {
         if (var1 instanceof Human) {
            Human var2 = (Human)var1;
            if (Intrinsics.areEqual(this.name, var2.name) && this.age == var2.age) {
               return true;
            }
         }

         return false;
      } else {
         return true;
      }
   }
}
  • Kotlin은 원래 java의 static에 해당하는 키워드가 존재하지 않는다

  • 하지만, 워낙 많이 사용하는 디자인 패턴 object키워드로 클래스를 생성하면 된다

  • Singleton.kt

class Point(var x:Double, var y:Double)

object PointFactory{
    fun NewCarterianPoint(x: Double, y: Double) : Point {
        return Point(x,y)
    }
    fun NewPolarPoint(rho: Double, theta: Double) : Point {
        return Point(rho*Math.cos(theta), rho*Math.sin(theta))
    } 
}

fun main(args: Array<String>){
    var point: Point = PointFactory.NewCarterianPoint(1.1, 2.2) 
    print(point)
}
  • Singleton.decompile.java
// SingletoneKt.java
import kotlin.Metadata;
import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;

@Metadata(
   mv = {1, 1, 7},
   bv = {1, 0, 2},
   k = 2,
   d1 = {"\u0000\u0014\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0010\u0011\n\u0002\u0010\u000e\n\u0002\b\u0002\u001a\u0019\u0010\u0000\u001a\u00020\u00012\f\u0010\u0002\u001a\b\u0012\u0004\u0012\u00020\u00040\u0003¢\u0006\u0002\u0010\u0005¨\u0006\u0006"},
   d2 = {"main", "", "args", "", "", "([Ljava/lang/String;)V", "production sources for module FirstProject"}
)
public final class SingletoneKt {
   public static final void main(@NotNull String[] args) {
      Intrinsics.checkParameterIsNotNull(args, "args");
      Point point = PointFactory.INSTANCE.NewCarterianPoint(1.1D, 2.2D);
      System.out.print(point);
   }
}
// Point.java
import kotlin.Metadata;

@Metadata(
   mv = {1, 1, 7},
   bv = {1, 0, 2},
   k = 1,
   d1 = {"\u0000\u0012\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0010\u0006\n\u0002\b\t\u0018\u00002\u00020\u0001B\u0015\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u0012\u0006\u0010\u0004\u001a\u00020\u0003¢\u0006\u0002\u0010\u0005R\u001a\u0010\u0002\u001a\u00020\u0003X\u0086\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b\u0006\u0010\u0007\"\u0004\b\b\u0010\tR\u001a\u0010\u0004\u001a\u00020\u0003X\u0086\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b\n\u0010\u0007\"\u0004\b\u000b\u0010\t¨\u0006\f"},
   d2 = {"LPoint;", "", "x", "", "y", "(DD)V", "getX", "()D", "setX", "(D)V", "getY", "setY", "production sources for module FirstProject"}
)
public final class Point {
   private double x;
   private double y;

   public final double getX() {
      return this.x;
   }

   public final void setX(double var1) {
      this.x = var1;
   }

   public final double getY() {
      return this.y;
   }

   public final void setY(double var1) {
      this.y = var1;
   }

   public Point(double x, double y) {
      this.x = x;
      this.y = y;
   }
}
// PointFactory.java
import kotlin.Metadata;
import org.jetbrains.annotations.NotNull;

@Metadata(
   mv = {1, 1, 7},
   bv = {1, 0, 2},
   k = 1,
   d1 = {"\u0000\u001a\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0006\n\u0002\b\u0005\bÆ\u0002\u0018\u00002\u00020\u0001B\u0007\b\u0002¢\u0006\u0002\u0010\u0002J\u0016\u0010\u0003\u001a\u00020\u00042\u0006\u0010\u0005\u001a\u00020\u00062\u0006\u0010\u0007\u001a\u00020\u0006J\u0016\u0010\b\u001a\u00020\u00042\u0006\u0010\t\u001a\u00020\u00062\u0006\u0010\n\u001a\u00020\u0006¨\u0006\u000b"},
   d2 = {"LPointFactory;", "", "()V", "NewCarterianPoint", "LPoint;", "x", "", "y", "NewPolarPoint", "rho", "theta", "production sources for module FirstProject"}
)
public final class PointFactory {
   public static final PointFactory INSTANCE;

   @NotNull
   public final Point NewCarterianPoint(double x, double y) {
      return new Point(x, y);
   }

   @NotNull
   public final Point NewPolarPoint(double rho, double theta) {
      return new Point(rho * Math.cos(theta), rho * Math.sin(theta));
   }

   private PointFactory() {
      INSTANCE = (PointFactory)this;
   }

   static { // 객체가 생성될 때 한번만 수행
      new PointFactory();
   }
}
  • 클래스 내부에서 사용할때는 companion object 키워드를 사용

  • CompanionObjects.kt

package KotlinSamples.CompanionObjects

interface IFactory<T>{
	fun create() : T
}

data class Point(var x:Double, var y:Double){
	companion object : IFactory<Point>{
		override fun create(): Point {
			return Point(9.0, 0.0)
		}
		fun createCarterian(x:Double, y:Double): Point = Point(x,y)
		fun createPolar(x:Double, y:Double): Point = Point(x,y)
	}
}

fun main(args: Array<String>){
	var simplePoint = Point(1.0, 2.0)
	println(simplePoint);

	var factoryPoint2 = Point.createPolar(3.0, Math.PI/2)
	println(factoryPoint2);

	var defaultPoint = Point.Companion.create()
	println(defaultPoint);
}
  • CompanionObjects.decompile.java
// IFactory.java
package KotlinSamples.CompanionObjects;

import kotlin.Metadata;

@Metadata(
   mv = {1, 1, 7},
   bv = {1, 0, 2},
   k = 1,
   d1 = {"\u0000\u000e\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0000\n\u0002\b\u0003\bf\u0018\u0000*\u0004\b\u0000\u0010\u00012\u00020\u0002J\r\u0010\u0003\u001a\u00028\u0000H&¢\u0006\u0002\u0010\u0004¨\u0006\u0005"},
   d2 = {"LKotlinSamples/CompanionObjects/IFactory;", "T", "", "create", "()Ljava/lang/Object;", "production sources for module FirstProject"}
)
public interface IFactory {
   Object create();
}
// Point.java
package KotlinSamples.CompanionObjects;

import kotlin.Metadata;
import kotlin.jvm.internal.DefaultConstructorMarker;
import org.jetbrains.annotations.NotNull;

@Metadata(
   mv = {1, 1, 7},
   bv = {1, 0, 2},
   k = 1,
   d1 = {"\u0000(\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0010\u0006\n\u0002\b\f\n\u0002\u0010\u000b\n\u0002\b\u0002\n\u0002\u0010\b\n\u0000\n\u0002\u0010\u000e\n\u0002\b\u0002\b\u0086\b\u0018\u0000 \u00162\u00020\u0001:\u0001\u0016B\u0015\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u0012\u0006\u0010\u0004\u001a\u00020\u0003¢\u0006\u0002\u0010\u0005J\t\u0010\f\u001a\u00020\u0003\u0003J\t\u0010\r\u001a\u00020\u0003\u0003J\u001d\u0010\u000e\u001a\u00020\u00002\b\b\u0002\u0010\u0002\u001a\u00020\u00032\b\b\u0002\u0010\u0004\u001a\u00020\u0003\u0001J\u0013\u0010\u000f\u001a\u00020\u00102\b\u0010\u0011\u001a\u0004\u0018\u00010\u0001\u0003J\t\u0010\u0012\u001a\u00020\u0013\u0001J\t\u0010\u0014\u001a\u00020\u0015\u0001R\u001a\u0010\u0002\u001a\u00020\u0003X\u0086\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b\u0006\u0010\u0007\"\u0004\b\b\u0010\tR\u001a\u0010\u0004\u001a\u00020\u0003X\u0086\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b\n\u0010\u0007\"\u0004\b\u000b\u0010\t¨\u0006\u0017"},
   d2 = {"LKotlinSamples/CompanionObjects/Point;", "", "x", "", "y", "(DD)V", "getX", "()D", "setX", "(D)V", "getY", "setY", "component1", "component2", "copy", "equals", "", "other", "hashCode", "", "toString", "", "Companion", "production sources for module FirstProject"}
)
public final class Point {
   private double x;
   private double y;
   public static final Point.Companion Companion = new Point.Companion((DefaultConstructorMarker)null);

   public final double getX() {
      return this.x;
   }

   public final void setX(double var1) {
      this.x = var1;
   }

   public final double getY() {
      return this.y;
   }

   public final void setY(double var1) {
      this.y = var1;
   }

   public Point(double x, double y) {
      this.x = x;
      this.y = y;
   }

   public final double component1() {
      return this.x;
   }

   public final double component2() {
      return this.y;
   }

   @NotNull
   public final Point copy(double x, double y) {
      return new Point(x, y);
   }

   // $FF: synthetic method
   // $FF: bridge method
   @NotNull
   public static Point copy$default(Point var0, double var1, double var3, int var5, Object var6) {
      if ((var5 & 1) != 0) {
         var1 = var0.x;
      }

      if ((var5 & 2) != 0) {
         var3 = var0.y;
      }

      return var0.copy(var1, var3);
   }

   public String toString() {
      return "Point(x=" + this.x + ", y=" + this.y + ")";
   }

   public int hashCode() {
      long var10000 = Double.doubleToLongBits(this.x);
      int var1 = (int)(var10000 ^ var10000 >>> 32) * 31;
      long var10001 = Double.doubleToLongBits(this.y);
      return var1 + (int)(var10001 ^ var10001 >>> 32);
   }

   public boolean equals(Object var1) {
      if (this != var1) {
         if (var1 instanceof Point) {
            Point var2 = (Point)var1;
            if (Double.compare(this.x, var2.x) == 0 && Double.compare(this.y, var2.y) == 0) {
               return true;
            }
         }

         return false;
      } else {
         return true;
      }
   }

   @Metadata(
      mv = {1, 1, 7},
      bv = {1, 0, 2},
      k = 1,
      d1 = {"\u0000\u0018\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0004\n\u0002\u0010\u0006\n\u0002\b\u0003\b\u0086\u0003\u0018\u00002\b\u0012\u0004\u0012\u00020\u00020\u0001B\u0007\b\u0002¢\u0006\u0002\u0010\u0003J\b\u0010\u0004\u001a\u00020\u0002H\u0016J\u0016\u0010\u0005\u001a\u00020\u00022\u0006\u0010\u0006\u001a\u00020\u00072\u0006\u0010\b\u001a\u00020\u0007J\u0016\u0010\t\u001a\u00020\u00022\u0006\u0010\u0006\u001a\u00020\u00072\u0006\u0010\b\u001a\u00020\u0007¨\u0006\n"},
      d2 = {"LKotlinSamples/CompanionObjects/Point$Companion;", "LKotlinSamples/CompanionObjects/IFactory;", "LKotlinSamples/CompanionObjects/Point;", "()V", "create", "createCarterian", "x", "", "y", "createPolar", "production sources for module FirstProject"}
   )
   public static final class Companion implements IFactory {
      @NotNull
      public Point create() {
         return new Point(9.0D, 0.0D);
      }

      // $FF: synthetic method
      // $FF: bridge method
      public Object create() {
         return this.create();
      }

      @NotNull
      public final Point createCarterian(double x, double y) {
         return new Point(x, y);
      }

      @NotNull
      public final Point createPolar(double x, double y) {
         return new Point(x, y);
      }

      private Companion() {
      }

      // $FF: synthetic method
      public Companion(DefaultConstructorMarker $constructor_marker) {
         this();
      }
   }
}
// CompanionObjectsKt.java
package KotlinSamples.CompanionObjects;

import kotlin.Metadata;
import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;

@Metadata(
   mv = {1, 1, 7},
   bv = {1, 0, 2},
   k = 2,
   d1 = {"\u0000\u0014\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0010\u0011\n\u0002\u0010\u000e\n\u0002\b\u0002\u001a\u0019\u0010\u0000\u001a\u00020\u00012\f\u0010\u0002\u001a\b\u0012\u0004\u0012\u00020\u00040\u0003¢\u0006\u0002\u0010\u0005¨\u0006\u0006"},
   d2 = {"main", "", "args", "", "", "([Ljava/lang/String;)V", "production sources for module FirstProject"}
)
public final class CompanionObjectsKt {
   public static final void main(@NotNull String[] args) {
      Intrinsics.checkParameterIsNotNull(args, "args");
      Point simplePoint = new Point(1.0D, 2.0D);
      System.out.println(simplePoint);
      Point factoryPoint2 = Point.Companion.createPolar(3.0D, 1.5707963267948966D);
      System.out.println(factoryPoint2);
      Point defaultPoint = Point.Companion.create();
      System.out.println(defaultPoint);
   }
}
⚠️ **GitHub.com Fallback** ⚠️