publicclassVector3i {public val x as int;public val y as int;public val z as int;publicthis(x as int, y as int, z as int) {this.x= x;this.y= y;this.z= z; }public +(other as Vector3i) as Vector3i {returnnewVector3i(this.x+other.x,this.y+other.y,this.z+other.z); }public -(other as Vector3i) as Vector3i {returnnewVector3i(this.x-other.x,this.y-other.y,this.z-other.z); }public *(multiply as int) as Vector3i {returnnewVector3i(this.x* multiply,this.y* multiply,this.z* multiply); }public ~(other as Vector3i) as int {returnthis.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 =newVector3i(1,2,3);val vb =newVector3i(4,5,6);println(va + vb); // 我们重载了加号运算符,现在可以用加号进行向量的加法运算了。