System.Runtime.Serialization.StreamingContext sc): base(si, sc) { }
}
// , , public interface IPhoneNumber {
string Number { get; set;
}
string Name { get; set;
}
}
// .
// IPhoneNumber. class Friend: IPhoneNumber {
public Friend(string n, string num, bool wk) {
Name = n;
Number = num;
IsWorkNumber = wk;
}
public bool IsWorkNumber { get; private set; }
// IPhoneNumber. public string Number { get; set; } public string Name { get; set; }
//...
}
// , class Supplier: IPhoneNumber {
public Supplier(string n, string num) {
Name = n;
Number = num;
}
// IPhoneNumber. public string Number { get; set; } public string Name { get; set; }
//...
}
// IPhoneNumber , class EmailFriend {
//...
}
// PhoneList // , , PhoneNumber. class PhoneList<T> where T: IPhoneNumber {
T[] phList; int end;
public PhoneList() { phList = new T[10]; end = 0;
}
public bool-Add(T newEntry) { if(end == 10) return false;
phList[end] = newEntry; end++;
Return true;
}
// , public FindByName(string name) {
for(int i=0; i<end; i++) {
// , Name // IPhoneNumber, // , if(phList[i].Name == name) return phList[i];
}
// , throw new NotFoundException();
}
// , public FindByNumber(string number) { for(int i=0; i<end; i++) {
// , // Number IPhoneNumber, // .
if(phList[i].Number == number) return phList[i];
}
// , throw new NotFoundException ();
}
//...
}
// , class UselnterfaceConstraint { static void Main() {
// , // Friend IPhoneNumber. PhoneList<Friend> plist = new PhoneList<Friend>(); plist.Add(new Friend("", "555-1234", true)); plist.Add(new Friend("", "555-6756", true)); plist.Add(new Friend("", "555-9254", false));
try {
// .
Friend frnd = plist.FindByName("");
Console.Write(frnd.Name + ": " + frnd.Number);
If(frnd.IsWorkNumber)
Console.WriteLine (" ()"); else
Console.WriteLine();
} catch(NotFoundException) {
Console.WriteLine("He ");
|
|
}
Console.WriteLine();
// , Supplier // IPhoneNumber.
PhoneList<Supplier> plist2 = new PhoneList<Supplier>(); plist2.Add(new Supplier(" Global Hardware", "555-8834")); plist2.Add(new Supplier(" Computer Warehouse", "555-9256")); plist2.Add(new Supplier(" NetworkCity", "555-2564"));
try {
// . Supplier sp = plist2.FindByNumber("555-2564");
Console.WriteLine(sp.Name + ": " + sp.Number);
} catch(NotFoundException) {
Console.WriteLine("He ");
}
// EmailFriend IPhoneNumber.
// PhoneList<EmailFriend> plist3 =
// new PhoneList<EmailFriend>(); // !
}
}
, PhoneList, , IPhoneList. , Friend Supplier, , . EmailFriend, . , Main (). , .
new ()
new () . , . new (), , . , , . new (), .