c# 定义operator运算符

        public void TestFunc()
        {
            Complex complex1 = new Complex();
            Complex complex2 = new Complex();
            var s = complex1 + complex2;
        }


    }
    public class Complex
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public Complex()
        {

        }
        // 必须在当前类中定义
        public static Complex operator +(Complex a, Complex b)
        {
            return new Complex() { Name = a.Name + b.Name, Age = a.Age + b.Age };
        }
        public static string operator -(Complex a, Complex b)
        {
            return null;
        }
        //public static Boolean operator ==(Complex a, Complex b)
        //{
        //    return true;
        //}
        //public static Boolean operator !=(Complex a, Complex b)
        //{
        //    return false;
        //}
        //public override bool Equals(object obj)
        //{
        //    return base.Equals(obj);
        //}
        //public override int GetHashCode()
        //{
        //    return base.GetHashCode();
        //}
    }
}

 

posted @ 2018-05-30 23:01  谁说程序猿很猥琐  阅读(8221)  评论(0编辑  收藏  举报