【題目描述】
【我的解答】
using System;
using System.Collections;
namespace Ch11_Exercises
{
class Person:ICloneable
{
private string name;
private int age;
public Person() : this("default Name", 20)
{
}
public Person(string name, int age)
{
this.name = name;
this.age = age;
}
public string Name
{
get { return name; }
set { name = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
public static bool operator >=(Person p1, Person p2)
{
return p1.Age >= p2.Age;
}
public static bool operator <(Person p1, Person p2)
{
return !(p1>=p2);
}
public static bool operator <=(Person p1,Person p2)
{
return p1.Age <= p2.Age;
}
public static bool operator >(Person p1, Person p2)
{
return !(p1<=p2);
}
/*
public static bool operator ==(Person p1,Person p2)
{
return p1.Age == p2.Age;
}
public static bool operator !=(Person p1,Person p2)
{
return !(p1 == p2);
}
public override bool Equals(object obj)
{
if(obj is Person)
{
return age == (obj as Person).age;
}
else
{
throw new ArgumentException("Cannot compare Person objects with objects of type"
+obj.GetType().ToString());
}
}
public override int GetHashCode()
{
return age;
}
*/
public object Clone()
{
return this.MemberwiseClone();
}
}
class People :DictionaryBase,ICloneable
{
public IEnumerable Ages
{
get
{
foreach(object p in Dictionary.Values)
{
yield return (p as Person).Age;
}
}
}
public void Add(Person newPerson)
{
Dictionary.Add(newPerson.Name,newPerson);
}
public void Remove(string newID)
{
Dictionary.Remove(newID);
}
public Person this[string personName]
{
get { return (Person)Dictionary[personName]; }
set { Dictionary[personName] = value; }
}
public Person[] GetOldest()
{
Person oldestPerson = null;
People oldestPeople = new People();
Person currentPerson;
foreach(DictionaryEntry p in Dictionary)
{
currentPerson = p.Value as Person;
if(oldestPerson == null)//最好別重載==,因爲這裏我們不希望重載
{
oldestPerson = currentPerson;
oldestPeople.Add(currentPerson);
}
else
{
if (currentPerson > oldestPerson)
{
oldestPerson = currentPerson;
oldestPeople.Clear();
oldestPeople.Add(currentPerson);
}
else if(currentPerson >= oldestPerson)//未重載==號
{
oldestPeople.Add(currentPerson);
}
}
}
Person[] ret = new Person[oldestPeople.Count];
int copyIndex = 0;
foreach(DictionaryEntry p in oldestPeople)
{
ret[copyIndex++] = p.Value as Person;
}
return ret;
}
public object Clone()
{
People people = new People();
foreach(DictionaryEntry p in Dictionary)
{
people.Add((p.Value as Person).Clone() as Person);
}
return people;
}
/* //shallow copy
public object Clone()
{
return this.MemberwiseClone();
}
*/
}
class Test
{
static void Main(string[] args)
{
People personCollection = new People();
personCollection.Add(new Person("QiuQiu",20));
personCollection.Add(new Person("BiuBiu", 21));
personCollection.Add(new Person("PiuPiu", 21));
personCollection.Add(new Person("NiuNiu", 16));
Console.WriteLine("members in personCollection:");
foreach (DictionaryEntry p in personCollection)
{
Person cur = p.Value as Person;
Console.WriteLine($"Name:{cur.Name},Age:{cur.Age}");
}
Console.WriteLine("01:Search personCollection[\"PiuPiu\"]");
Console.WriteLine($"Name:{personCollection["PiuPiu"].Name},Age:" +
$"{personCollection["PiuPiu"].Age}");
//personCollection.Remove("PiuPiu");
Console.WriteLine("02: is BiuBiu younger than QiuQiu?");
if (personCollection["BiuBiu"] < personCollection["QiuQiu"])
Console.WriteLine("yes");
else
Console.WriteLine("no");
//3
Console.WriteLine("03:OldestPerson");
Person[] oldests = personCollection.GetOldest();
foreach(Person p in oldests)
{
Console.WriteLine($"name:{p.Name},Age:{p.Age}");
}
//4
Console.WriteLine("04:Deep Copy from people to newPeople");
People newPeople = personCollection.Clone() as People;
try
{
personCollection.Remove("PiuPiu");
personCollection["QiuQiu"].Name = "XiuXiu";
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine("people:");
foreach (DictionaryEntry p in personCollection)
{
Console.WriteLine($"Name:{(p.Value as Person).Name}," +
$"Age:{(p.Value as Person).Age}");
}
Console.WriteLine("newPeople:");
foreach(DictionaryEntry p in newPeople)
{
Console.WriteLine($"Name:{(p.Value as Person).Name}," +
$"Age:{(p.Value as Person).Age}");
}
Console.WriteLine("05:IEnumerable");
Console.WriteLine("personCollection Ages:");
foreach(int age in personCollection.Ages)
{
Console.WriteLine(age.ToString());
}
Console.ReadKey();
}
}
}
【Output】