标签:
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 6 IListTable i; 7 Contact c = new Contact(); 8 c.Name = "name1"; 9 Console.WriteLine(c.ColnumName); 10 11 Appointment a = new Appointment(); 12 a.Name = "name2"; 13 Console.WriteLine(((IListTable)a).ColnumName); 14 15 Console.ReadLine(); 16 17 } 18 } 19 20 interface IListTable 21 { 22 string ColnumName 23 { get; } 24 } 25 public class Contact : IListTable 26 { 27 public string Name 28 { 29 get 30 { 31 return FirtstName; 32 } 33 set 34 { 35 FirtstName = value + " from Contact"; 36 } 37 } 38 private string FirtstName; 39 //隐式实现 40 public string ColnumName 41 { 42 get { return FirtstName + " ColnumName"; } 43 set { FirtstName = value; } 44 } 45 46 47 } 48 public class Appointment : IListTable 49 { 50 public string Name 51 { 52 get 53 { 54 return FirtstName; 55 } 56 set 57 { 58 FirtstName = value + " from Appointment"; 59 } 60 } 61 private string FirtstName; 62 63 //显式实现 64 string IListTable.ColnumName 65 { 66 get { return FirtstName + " ColnumName"; } 67 //set { FirtstName = value; }//错误 68 } 69 70 }
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 6 7 } 8 } 9 10 public interface IReadAbleSettingsProvider 11 { 12 string GetSetting(string name, string defaultValue); 13 } 14 public interface ISettingsProvider : IReadAbleSettingsProvider 15 { 16 void SetSetting(string name, string value); 17 } 18 class FileSettingsProvider : ISettingsProvider 19 { 20 public void SetSetting(string name, string value) 21 { 22 // 23 } 24 string IReadAbleSettingsProvider.GetSetting(string name, string defaultValue) 25 { 26 // 27 return " "; 28 } 29 } 30
1 class FileSettingsProvider : ISettingsProvider, IReadAbleSettingsProvider 2 { 3 public void SetSetting(string name, string value) 4 { 5 // 6 } 7 string IReadAbleSettingsProvider.GetSetting(string name, string defaultValue) 8 { 9 // 10 return " "; 11 } 12 }
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 6 Contact[] items = new Contact[] { new Contact(), new Contact() }; 7 for (int i = 0; i < items.Length; i++) 8 { 9 items[i].ColnumName = "name" + i; 10 } 11 items.ListColumn(); 12 13 14 15 Console.ReadLine(); 16 17 } 18 } 19 20 interface IListTable 21 { 22 string ColnumName 23 { get; } 24 } 25 public class Contact : IListTable 26 { 27 public string Name 28 { 29 get 30 { 31 return FirtstName; 32 } 33 set 34 { 35 FirtstName = value + " from Contact"; 36 } 37 } 38 private string FirtstName; 39 //隐式实现 40 public string ColnumName 41 { 42 get { return FirtstName + " ColnumName"; } 43 set { FirtstName = value; } 44 } 45 46 47 } 48 49 static class Listable 50 { 51 public static void ListColumn(this IListTable[] items) 52 { 53 string headers = ""; 54 for (int i = 0; i < items.Length; i++) 55 { 56 headers += items[i].ColnumName + ","; 57 } 58 Console.WriteLine(headers); 59 } 60 }
1 public class PdaItem 2 { 3 public PdaItem() 4 { 5 } 6 public PdaItem(DateTime pLastUpdated) 7 { 8 9 LastUpdated = pLastUpdated; 10 } 11 public DateTime LastUpdated { set; get; } 12 } 13 interface IPerson 14 { 15 string FirstName { set; get; } 16 string LastName { set; get; } 17 } 18 public class Person : IPerson 19 { 20 public string Address { set; get; } 21 public string Phone { set; get; } 22 23 public string FirstName { set; get; } 24 public string LastName { set; get; } 25 } 26 //使用聚合(实现继承Person 和 PdaItem 两个类,并将方法和属性放到接口当中 27 public class Contact : PdaItem, IPerson 28 { 29 private Person _Person; 30 public Person person 31 { 32 set 33 { 34 _Person = value; 35 } 36 get 37 { 38 return _Person; 39 } 40 } 41 public string FirstName 42 { 43 get 44 { 45 return _Person.FirstName; 46 } 47 set 48 { 49 _Person.FirstName = value; 50 } 51 } 52 public string LastName 53 { 54 get 55 { 56 return _Person.LastName; 57 } 58 set 59 { 60 _Person.LastName = value; 61 } 62 } 63 64 }
标签:
原文地址:http://www.cnblogs.com/tlxxm/p/4604528.html