美文网首页
C#运算符重载

C#运算符重载

作者: 晓龙酱 | 来源:发表于2017-09-18 10:33 被阅读6次
    class Salary{
    
        public Salary(int val){
            this.val = val;
        }
    
        public int val
        {
            private set;
            get;
        }
    
        override public string ToString()
        {
            return val.ToString();
        }
    <font color=blue>
        static public Salary operator +(Salary a, Salary b){
            return new Salary(a.val + b.val);
        }
    
        static public Salary operator -(Salary a, Salary b){
            return new Salary(a.val - b.val);
        }
    </font>
    }
    
    Salary s1 = new Salary(1);
    Salary s2 = new Salary(2);
    Salary r1 = s1 + s2;        // 3
    Salary r2 = s1 - s2;        // -1
    

    相关文章

      网友评论

          本文标题:C#运算符重载

          本文链接:https://www.haomeiwen.com/subject/fqivsxtx.html