public class Vector3i {
public val x as int;
public val y as int;
public val z as int;
public this(x as int, y as int, z as int) {
this.x = x;
this.y = y;
this.z = z;
}
public +(other as Vector3i) as Vector3i {
return new Vector3i(this.x + other.x, this.y + other.y, this.z + other.z);
}
public -(other as Vector3i) as Vector3i {
return new Vector3i(this.x - other.x, this.y - other.y, this.z - other.z);
}
public *(multiply as int) as Vector3i {
return new Vector3i(this.x * multiply, this.y * multiply, this.z * multiply);
}
public ~(other as Vector3i) as int {
return this.x * other.x + this.y * other.y + this.z * other.z;
}
public *(other as Vector3i) as Vector3i {
return new Vector3i(this.y * other.z - this.z * other.y, this.z * other.x - this.x * other.z, this.x * other.y - this.y * other.x);
}
public implicit as string {
return "Vector3i[" + this.x + ", " + this.y + ", " + this.z + "]";
}
}
使用
val va = new Vector3i(1, 2, 3);
val vb = new Vector3i(4, 5, 6);
println(va + vb); // 我们重载了加号运算符,现在可以用加号进行向量的加法运算了。
// [Native] 表示调用内部 Java 方法,请忽略
[Native("getAtIndex")]
public [](index as usize) as T;
[Native("setAtIndex")]
public []=(index as usize, value as T) as T;
这样,对于一个 List<T> ,我们就可以用 list[1]list[1] = bar 的形式了。