SpringDragon

导航

 

  关键字:operator

  运算符重载是一个赋予运算符其他的含义的方法

  能重载的运算符:

    +、—、*、/、%、++、——

    ==、!=、>、<、>=、<=

    &、|、!、~(按位取反)

    赋值运算符不能重载

  对于关系运算符:

    重载关系运算符的时候,必须把配套的另外的一个运算符也重载了

    关系运算的重载方法的返回值为bool

    运算符重载的方法的参数个数不是随意的

// 通过 == 来判断两个矩形的面积是否相同
    public static bool operator ==(Rect r0, Rect r1) {
        double a0 = r0.width * r0.length;
        double a1 = r1.width * r1.length;
        bool result = a0 == a1;
        return result;
    }
    public static bool operator !=(Rect r0, Rect r1) {
        double a0 = r0.width * r0.length;
        double a1 = r1.width * r1.length;
        return a0 != a1;
    }
}

  

posted on 2017-05-06 13:35  chenquanlong  阅读(151)  评论(0编辑  收藏  举报