专注于高品质PHP技术等信息服务于一体 [STIEMAP] [RSS]

百度提供的广告:
c#
当前位置:首页 > 技术文档 > c# >  > 
C#运算符重载简单实例

using System;
using System.Collections.Generic;
using System.Text;

namespace 运算符重载
{
    class Program
    {
        static void Main(string[] args)
        {
            test t1,t2,t3;
            t1 = new test(5);
            t2 = new test(4);
            t3 = t1 + t2;
            System.Console.WriteLine(t3);

        }
    }
   
    class test
    {
        private  int x;

        public int X
        {
            get { return x; }
            set { x = value; }
        }

        public test()
        {
        }
        public test(int x)
        {
            X=x;
        }
        public test(int x,int x2)
        {
            X = x + x2;
        }
        public override string ToString()
        {
            return "运算结果是:"+X;
        }
        public static test operator+(test t1,test t2)
        {
            test t3 = new test(t1.X,t2.X);
            return t3;

        }
    }

}