您的位置:首页 > 运维架构

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();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐