// 10.8 操作符重载
public class Person 
...{
public string Name ;
public int Age ;
// public bool dy(int bAge)
// {
// return this.Age > bAge ;
// }
// public bool dy(Person b)
// {
// return this.Age > b.Age ;
// }
public static bool operator < (Person a ,Person b) /**/////是第三者比较另外两个人,所以用静态
...{
return a.Age <b.Age ;
}
public static bool operator >(Person a,Person b)
...{
return a.Age >b.Age ;
}
}
class Class1
...{
static void Main(string[] args)
...{
Person a = new Person () ;
a.Age = 23 ;
Person b = new Person () ;
b.Age =25 ;
// if (a.dy (b))
// {
// System.Console.WriteLine("a>b") ;
// }
if(a<b)
...{
System.Console.WriteLine("a<b") ;
}
}
}
本文介绍了一个使用C#实现的操作符重载示例,通过定义Person类并重载小于号和大于号操作符来比较两个Person对象的年龄大小。
2613

被折叠的 条评论
为什么被折叠?



