kotlin 运算符重载解析

package com.abc
data class Point(val x: Int, val y: Int) {
    // 重载加法运算符 +
    operator fun plus(other: Point): Point {
        return Point(x + other.x, y + other.y)
    }

    fun plus1(other: Point): Point {
        return Point(x + other.x, y + other.y)
    }
}


fun main() {
    val point1 = Point(2, 3)
    val point2 = Point(1, 5)

    // 使用重载的加法运算符
    val sum = point1 + point2

    val sum2 = point1.plus1(point2)


}

反编译之后的java代码为:

// A123Kt.java
package com.abc;

import kotlin.Metadata;

@Metadata(
   mv = {1, 8, 0},
   k = 2,
   d1 = {"\u0000\b\n\u0000\n\u0002\u0010\u0002\n\u0000\u001a\u0006\u0010\u0000\u001a\u00020\u0001¨\u0006\u0002"},
   d2 = {"main", "", "OneApp_release"}
)
public final class A123Kt {
   public static final void main() {
      Point point1 = new Point(2, 3);
      Point point2 = new Point(1, 5);
      point1.plus(point2);
      point1.plus1(point2);
   }

   // $FF: synthetic method
   public static void main(String[] var0) {
      main();
   }
}
// Point.java
package com.abc;

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

@Metadata(
   mv = {1, 8, 0},
   k = 1,
   d1 = {"\u0000 \n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0010\b\n\u0002\b\t\n\u0002\u0010\u000b\n\u0002\b\u0005\n\u0002\u0010\u000e\n\u0000\b\u0086\b\u0018\u00002\u00020\u0001B\u0015\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u0012\u0006\u0010\u0004\u001a\u00020\u0003¢\u0006\u0002\u0010\u0005J\t\u0010\t\u001a\u00020\u0003HÆ\u0003J\t\u0010\n\u001a\u00020\u0003HÆ\u0003J\u001d\u0010\u000b\u001a\u00020\u00002\b\b\u0002\u0010\u0002\u001a\u00020\u00032\b\b\u0002\u0010\u0004\u001a\u00020\u0003HÆ\u0001J\u0013\u0010\f\u001a\u00020\r2\b\u0010\u000e\u001a\u0004\u0018\u00010\u0001HÖ\u0003J\t\u0010\u000f\u001a\u00020\u0003HÖ\u0001J\u0011\u0010\u0010\u001a\u00020\u00002\u0006\u0010\u000e\u001a\u00020\u0000H\u0086\u0002J\u000e\u0010\u0011\u001a\u00020\u00002\u0006\u0010\u000e\u001a\u00020\u0000J\t\u0010\u0012\u001a\u00020\u0013HÖ\u0001R\u0011\u0010\u0002\u001a\u00020\u0003¢\u0006\b\n\u0000\u001a\u0004\b\u0006\u0010\u0007R\u0011\u0010\u0004\u001a\u00020\u0003¢\u0006\b\n\u0000\u001a\u0004\b\b\u0010\u0007¨\u0006\u0014"},
   d2 = {"Lcom/uurobot/voicesrc/Point;", "", "x", "", "y", "(II)V", "getX", "()I", "getY", "component1", "component2", "copy", "equals", "", "other", "hashCode", "plus", "plus1", "toString", "", "OneApp_release"}
)
public final class Point {
   private final int x;
   private final int y;

   @NotNull
   public final Point plus(@NotNull Point other) {
      Intrinsics.checkNotNullParameter(other, "other");
      return new Point(this.x + other.x, this.y + other.y);
   }

   @NotNull
   public final Point plus1(@NotNull Point other) {
      Intrinsics.checkNotNullParameter(other, "other");
      return new Point(this.x + other.x, this.y + other.y);
   }

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

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

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

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

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

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

   // $FF: synthetic method
   public static Point copy$default(Point var0, int var1, int var2, int var3, Object var4) {
      if ((var3 & 1) != 0) {
         var1 = var0.x;
      }

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

      return var0.copy(var1, var2);
   }

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

   public int hashCode() {
      return Integer.hashCode(this.x) * 31 + Integer.hashCode(this.y);
   }

   public boolean equals(@Nullable Object var1) {
      if (this != var1) {
         if (var1 instanceof Point) {
            Point var2 = (Point)var1;
            if (this.x == var2.x && this.y == var2.y) {
               return true;
            }
         }

         return false;
      } else {
         return true;
      }
   }
}


可以看到实际是生成了一个 plus方法,
然后我们把我们的plus1方法名称修改为plus ,编译报错,提示有2个同名方法。
完美 。

你可能感兴趣的:(kotlin,kotlin,开发语言,android)