C# 操作符重载

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:   
   6:  namespace ConsoleApplication1
   7:  {
   8:      class Program
   9:      {
  10:   
  11:          class CAdd
  12:          {
  13:              public int val;
  14:              public string str;
  15:              public bool bok;
  16:   
  17:              public static CAdd operator + (CAdd a, CAdd b)
  18:              {
  19:                  CAdd c = new CAdd();
  20:                  c.val = a.val + b.val;
  21:                  c.str = a.str + b.str;
  22:                  c.bok = a.bok && b.bok;
  23:                  return c;
  24:              }
  25:   
  26:          }
  27:   
  28:          static void Main(string[] args)
  29:          {
  30:              CAdd a = new CAdd();
  31:              CAdd b = new CAdd();
  32:              CAdd d = new CAdd();
  33:              
  34:              a.val = 3;
  35:              a.str = "1";
  36:              a.bok = true;
  37:   
  38:              b.val = 5;
  39:              b.str = "2";
  40:              b.bok = false;
  41:   
  42:              d.val = 5;
  43:              d.str = "2";
  44:              d.bok = false;
  45:   
  46:              CAdd c = a + b + d;
  47:              Console.ReadLine();
  48:          }
  49:      }
  50:  }
 
 


作者:GangWang
出处:http://www.cnblogs.com/GnagWang/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

 
原文地址:https://www.cnblogs.com/GnagWang/p/1693338.html