java中没有操作符重载的概念,下面的例子简单的说明操作符重载。
using System; namespace Welcome { class Program { static void Main(string[] args) { DKR d1 = new DKR(1, 2, 3); DKR d2 = new DKR(2, 3, 4); DKR d = d1 + d2; Console.WriteLine(d.x+" "+d.y+" "+d.z); Console.Read(); } } class DKR { public float x; public float y; public float z; public DKR(float x,float y,float z) { this.x = x; this.y = y; this.z = z; } /** * 操作符重载的一个例子,注意是几元操作符,那么它的形式参数就应该是几个。 */ public static DKR operator+(DKR d1,DKR d2) { DKR d = new DKR(0,0,0); d.x = d1.x + d2.x; d.y = d1.y + d2.y; d.z = d1.z + d2.z; return d; } } }
输出: