11_5_3给people添加GetOldest()方法,使用上面定义的重载运算符,返回一个Age最大的对象数组
2012-10-25 04:49
1061 查看
//file Person.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace _11_5_1 { class Person : CollectionBase { private string name; private int age; public string Name { get { return name; } set { name = value ; } } public int Age { get { return age; } set { age = value; } } public Person() { } public Person(string name, int age) { this.name = name; this.age = age; } public static bool operator > (Person per1,Person per2) { return (per1.Age>per2.Age); } public static bool operator<(Person per1,Person per2) { return (per1.Age < per2.Age); } public static bool operator <=(Person per1, Person per2) { return (per1.Age <= per2.Age); } public static bool operator >=(Person per1,Person per2) { return (per1.Age >= per2.Age); } } } //-------------------------------------------- //file People.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace _11_5_1 { class People : DictionaryBase { public void Add(string ID,Person person) { Dictionary.Add(ID,person); } public void Remove(string ID) { Dictionary.Remove(ID); } public Person this[string ID] { get { return (Person)Dictionary[ID]; } set { Dictionary[ID] = value; } } public People this[int Index] { get { return (People)this[Index]; } set { this[Index] = value; } } //---------------------question 3 public static Person GetOldest(People peo) { Person otherPerson = new Person(); foreach (DictionaryEntry myEntry in peo) { if (otherPerson != (Person)myEntry.Value) { otherPerson = ((Person)myEntry.Value); break; } } foreach(DictionaryEntry myEntry in peo) { if (otherPerson < (Person)myEntry.Value) { otherPerson = (Person)myEntry.Value; } } return otherPerson; } } } //---------------------------------------- //file Program.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _11_5_1 { class Program { static void Main(string[] args) { People people = new People(); Person per1 = new Person("Chuck", 24); Person per2 = new Person("Candy", 30); Person per3 = new Person("Bob", 29); people.Add("Chuck", per1); people.Add("Candy", per2); people.Add("Bob", per3); Console.WriteLine("{0} {1}", people["Chuck"].Name, people["Chuck"].Age); Console.WriteLine("{0} {1}", people["Candy"].Name, people["Candy"].Age); Console.WriteLine("-----------------------------"); //----------------------------------------------- if (per1 > per2) { Console.WriteLine("per1 > per2 of Age"); } if (per1 <= per2) { Console.WriteLine("per1 <= per2 of Age"); } Console.WriteLine("-----------------------------"); //---------------------------------------question Person per = new Person(); per = People.GetOldest(people); Console.WriteLine("{0} {1}", per.Name, per.Age); Console.ReadKey(); } } }
相关文章推荐
- 整数数组的定义,然后输入一个整数X,假定X不在这个数组,返回小于X位置的最大数目i而超过X位置的最小数目j
- [ASP]使用RecordSet对象的GetRows方法(返回而为数组)和RS.MaxRecords(指定记录集的最大容量)
- Hibernate查询,返回new对象(注意这个新定义的类要有构造函数),使用sql带条件分页查询并且把结果显示到一个对象的集里面的解决方案
- Visual C#2005——如何使用ToArray方法返回一个强类型数组
- JavaScript数组对象实现增加一个返回随机元素的方法
- 使用泛型, 写一个为任意类型的动态数组添加元素的方法
- 使用jQuery匹配文档中所有的li元素,返回一个jQuery对象,然后通过数组下标的方式读取jQuery集合中第1个DOM元素,此时返回的是DOM对象,然后调用DOM属性innerHTML,读取该元素 包含的文本信息
- Hibernate查询,返回new对象(注意这个新定义的类要有构造函数),使用sql带条件分页查询并且把结果显示到一个对象的集里面的解决方案
- 存储过程中使用RETURN语句返回数值,.Net里用ExecuteScalar方法结果将会导致一个未引用到具体对象的异常
- Visual C#2005——如何使用ToArray方法返回一个强类型数组
- Visual C#2005——如何使用ToArray方法返回一个强类型数组
- 前台使用ajax 调用action方法,返回一个对象List集合的json
- push() 方法将一个或多个元素添加到数组的末尾,并返回新数组的长度
- 用类名做方法的返回值类型 在学习Java的初始阶段,很多同学使用基本数据类型定义变量和引用类型定义变量以及使用类名做方法的返回值类型常常不知所以。今天我以自己的解读方式和个人的理解作一个简单的解释,和
- JavaScript使用push方法添加一个元素到数组末尾用法实例
- 给一个整数数组,对数组中的每个整数中的所有数字按照升序排列(如101排序后为011)请写一个方法,输出排序后的数组中的最大数。 例如有一个数组: 101、132、375,排序后11、123、357,
- JS数组一个优化小技巧和对象方法的使用
- 使用hadoop命令rcc生成Record 一个简单的方法来实现自己的定义writable对象
- JavaScript数组对象实现增加一个返回随机元素的方法
- 使用ADO.NET类及方法实现数据库的查询并返回一个Object 数组