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:  }
 
posted @ 2010-03-23 23:34  Gang.Wang  阅读(129)  评论(0编辑  收藏  举报