通常我们需要对class的相加,相减,相乘 等重载以适应需求, 如caml查询的时候,我们可以定义一个caml类,然后来操作这些查询.

首先,我们定义一个class为Test
public class Test

然后定义两个成员,一个int类型的ID,一个字符串类型的Name.

        public int ID;
public string Name;

然后定义构造函数

        public Test()
{
} public Test(int id)
{
this.ID = id;
} public Test(int id, string name)
{
this.ID = id;
this.Name = name;
}

重载两个class相加的运算符,

        public static Test operator +(Test t1, Test t2)
{
if (t2.Name!= null)
{
return new Test(t1.ID + t2.ID, t1.Name + t2.Name);
}
else
{
return new Test(t1.ID + t2.ID);
}
}

重载两个class的|运算,其他的运算符如(-,*,/,&)大家可以自己去试试.

        public static Test operator |(Test t1, Test t2)
{
//显示ID大的class
return new Test(t1.ID > t2.ID ? t1.ID:t2.ID);
}

下面写了一个对Test这个class的扩展方法,相等于这个class自带的成员方法. 扩展返回发的写法关键是this 后面带类型和参数

    internal static class Util
{
public static string Format(this Test t)
{
StringBuilder sb = new StringBuilder();
if (t.ID != null)
{
sb.AppendLine("ID:"+t.ID.ToString());
}
if (!string.IsNullOrEmpty(t.Name))
{
sb.AppendLine("Name:" + t.Name.ToString());
}
return sb.ToString();
}
}

调用这个方法:

    class Program
{
static void Main(string[] args)
{
//测试两个class相加
Test test1 = new Test();
Test test2 = new Test();
Console.WriteLine("两个class相加的结果为:"+(test1 +test2).Format());
Console.WriteLine("两个class比较值大的结果为:" + (test1 |test2).Format()); }
}

运行结果如下:

全部代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Charlie.ConsoleWindow
{
class Program
{
static void Main(string[] args)
{
//测试两个class相加
Test test1 = new Test();
Test test2 = new Test();
Console.WriteLine("两个class相加的结果为:"+(test1 +test2).Format());
Console.WriteLine("两个class比较值大的结果为:" + (test1 |test2).Format()); }
} public class Test
{
public int ID;
public string Name; public Test()
{
} public Test(int id)
{
this.ID = id;
} public Test(int id, string name)
{
this.ID = id;
this.Name = name;
} public static Test operator +(Test t1, Test t2)
{
if (t2.Name!= null)
{
return new Test(t1.ID + t2.ID, t1.Name + t2.Name);
}
else
{
return new Test(t1.ID + t2.ID);
}
} public static Test operator |(Test t1, Test t2)
{
//显示ID大的class
return new Test(t1.ID > t2.ID ? t1.ID:t2.ID);
}
} internal static class Util
{
public static string Format(this Test t)
{
StringBuilder sb = new StringBuilder();
if (t.ID != null)
{
sb.AppendLine("ID:"+t.ID.ToString());
}
if (!string.IsNullOrEmpty(t.Name))
{
sb.AppendLine("Name:" + t.Name.ToString());
}
return sb.ToString();
}
}
}

如有错误,请大家指正~~~~

最新文章

  1. HTML5 audio与video标签实现视频播放,音频播放
  2. King's Quest —— POJ1904(ZOJ2470)Tarjan缩点
  3. fw:sed的高级用法
  4. Android中修改状态栏的颜色和我们App的风格一致
  5. <转>梳理:提高前端性能方面的处理以及不足
  6. vbox android x86 分辨率
  7. C语言结构体的引入
  8. 仿百度自动补全jquery
  9. <display:column>属性解释
  10. Main方法的执行过程(转)
  11. MySQL数据库操作常用命令
  12. git怎么fork一个仓库并pull request
  13. python用字符串调用当前模块内的函数
  14. Spring mvc下Ajax获取JSON对象问题 406错误
  15. zookeeper安装和使用(Windows环境)
  16. K-mer分析
  17. Elasticsearch java客户端调用cat服务
  18. 咏南中间件新增MORMOT移动端演示
  19. mysql 试图
  20. 【学习笔记】--- 老男孩学Python,day18 面向对象------继承

热门文章

  1. Linq 合并数据并相加
  2. Part 16 Important concepts related to functions in sql server
  3. C# tostring 格式化输出 (转)
  4. zz 如何在Linux下创建与解压zip, tar, tar.gz和tar.bz2文件
  5. 简明Python中的一个小错误
  6. 3月7日 Maximum Subarray
  7. JS判断客户端是手机还是PC
  8. 在 linux x86-32 模式下分析内存映射流程
  9. linux中的文件属性
  10. js设计模式(5)---外观模式