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(1);
Test test2 = new Test(2);
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();
}
}
}