标签:
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 Contact contact = new Contact(); 6 PdaItem pda = new PdaItem(); 7 pda = contact; 8 } 9 } 10 public class PdaItem 11 { 12 public string Name { set; get; } 13 public DateTime LastUpdated { set; get; } 14 } 15 public class Contact : PdaItem 16 { 17 public string Address { set; get; } 18 public string Phone { set; get; } 19 }
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 PdaItem pda = new PdaItem("name1", DateTime.Now); 6 Contact contact = new Contact("name2", DateTime.Now.AddDays(2), "beijing", "110"); 7 pda = contact; 8 Person person = new Person("PersonName1", "tianjin"); 9 contact = person; 10 } 11 } 12 public class PdaItem 13 { 14 public PdaItem() 15 { 16 } 17 public PdaItem(string pName, DateTime pLastUpdated) 18 { 19 Name = pName; 20 LastUpdated = pLastUpdated; 21 } 22 public string Name { set; get; } 23 public DateTime LastUpdated { set; get; } 24 } 25 public class Contact : PdaItem 26 { 27 public Contact() 28 { 29 } 30 public Contact(string pName, DateTime pLastUpdated, string pAddress, string pPhone) 31 : base(pName, pLastUpdated) 32 { 33 Address = pAddress; 34 Phone = pPhone; 35 } 36 public string Address { set; get; } 37 public string Phone { set; get; } 38 } 39 public class Person 40 { 41 public Person(string pName, string pAddress) 42 { 43 Name = pName; 44 Address = pAddress; 45 } 46 public string Name { set; get; } 47 public string Address { set; get; } 48 /// <summary> 49 /// 隐式转换 50 /// </summary> 51 /// <param name="person"></param> 52 /// <returns></returns> 53 public static implicit operator Contact(Person person) 54 { 55 Contact c = new Contact(); 56 c.Name = person.Name; 57 c.Address = person.Address; 58 c.LastUpdated = DateTime.Today; 59 c.Phone = "120"; 60 return c; 61 } 62 }
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 Contact p = new Contact("xxm", DateTime.Now, 1); 6 Console.WriteLine(p.GetSex()); 7 Console.ReadLine(); 8 9 10 } 11 } 12 public class PdaItem 13 { 14 public PdaItem() 15 { 16 } 17 public PdaItem(string pName, DateTime pLastUpdated) 18 { 19 Name = pName; 20 LastUpdated = pLastUpdated; 21 } 22 public string Name { set; get; } 23 public DateTime LastUpdated { set; get; } 24 } 25 public class Person 26 { 27 public Person(int pSex) 28 { 29 Sex = pSex; 30 } 31 public int Sex { set; get; } 32 public string GetSex() 33 { 34 switch (Sex) 35 { 36 case 0: 37 return "女"; 38 case 1: 39 return "男"; 40 default: 41 return "未定义"; 42 } 43 } 44 45 46 } 47 public class Contact : PdaItem 48 { 49 public Contact() 50 { 51 } 52 public Contact(string pName, DateTime pLastUpdated, int sex) 53 : base(pName, pLastUpdated) 54 { 55 person = new Person(sex); 56 } 57 public Person person { set; get; } 58 public int Sex 59 { 60 set { person.Sex = value; } 61 get { return person.Sex; } 62 } 63 public string GetSex() 64 { 65 switch (Sex) 66 { 67 case 0: 68 return Name + "," + "女" + "," + LastUpdated; 69 case 1: 70 return Name + "," + "男" + "," + LastUpdated; ; 71 default: 72 return Name + "," + "未定义" + "," + LastUpdated; ; 73 } 74 } 75 }
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 PdaItem p = new PdaItem("pname", DateTime.Now.AddDays(-2)); 6 Contact c = new Contact("name1", DateTime.Now); 7 p = c; 8 p.Name = "name1"; 9 Console.WriteLine(p.Name); 10 11 12 } 13 } 14 public class PdaItem 15 { 16 public PdaItem() 17 { 18 } 19 public PdaItem(string pName, DateTime pLastUpdated) 20 { 21 Name = pName; 22 LastUpdated = pLastUpdated; 23 } 24 public virtual string Name { set; get; } 25 26 public DateTime LastUpdated { set; get; } 27 } 28 29 public class Contact : PdaItem 30 { 31 public override string Name 32 { 33 get 34 { 35 return FirtstName; 36 } 37 set 38 { 39 FirtstName = value + " from Contact"; 40 } 41 } 42 public string FirtstName; 43 public Contact() 44 { 45 } 46 public Contact(string pName, DateTime pLastUpdated) 47 : base(pName, pLastUpdated) 48 { 49 50 51 } 52 53 }
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 PdaItem p = new PdaItem("pname", DateTime.Now.AddDays(-2)); 6 Contact c = new Contact("name1", DateTime.Now); 7 p = c; 8 p.Name = "name1"; 9 Console.WriteLine(p.Name); 10 11 12 } 13 } 14 public class PdaItem 15 { 16 public PdaItem() 17 { 18 } 19 public PdaItem(string pName, DateTime pLastUpdated) 20 { 21 Name = pName; 22 LastUpdated = pLastUpdated; 23 } 24 public string Name { set; get; } 25 26 public DateTime LastUpdated { set; get; } 27 } 28 29 public class Contact : PdaItem 30 { 31 public new string Name 32 { 33 get 34 { 35 return FirtstName; 36 } 37 set 38 { 39 FirtstName = value + " from Contact"; 40 } 41 } 42 public string FirtstName; 43 public Contact() 44 { 45 } 46 public Contact(string pName, DateTime pLastUpdated) 47 : base(pName, pLastUpdated) 48 { 49 50 51 } 52 53 }
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 PdaItem p; 6 Contact c = new Contact("contact name"); 7 p = c; 8 Console.WriteLine(p.Name +","+ p.GetSummary()); 9 10 Appointment ap = new Appointment("appointment name"); 11 p = ap; 12 Console.WriteLine(p.Name + "," + p.GetSummary()); 13 Console.ReadLine(); 14 15 16 } 17 } 18 public abstract class PdaItem 19 { 20 public PdaItem() 21 { 22 23 } 24 public PdaItem(string pName) 25 { 26 Name = pName; 27 } 28 public virtual string Name { set; get; } 29 public abstract string GetSummary(); 30 } 31 32 public class Contact : PdaItem 33 { 34 public new string Name 35 { 36 get 37 { 38 return FirtstName; 39 } 40 set 41 { 42 FirtstName = value + " from Contact"; 43 } 44 } 45 public string FirtstName; 46 public Contact() 47 { 48 } 49 public Contact(string pName) 50 : base(pName) 51 { 52 53 54 } 55 public override string GetSummary() 56 { 57 return "GetSummary() from Contact"; 58 } 59 60 } 61 public class Appointment : PdaItem 62 { 63 public new string Name 64 { 65 get 66 { 67 return FirtstName; 68 } 69 set 70 { 71 FirtstName = value + " from Appointment"; 72 } 73 } 74 public string FirtstName; 75 public Appointment() 76 { 77 } 78 public Appointment(string pName) 79 : base(pName) 80 { 81 82 83 } 84 public override string GetSummary() 85 { 86 return "GetSummary() from Appointment"; 87 } 88 89 }
标签:
原文地址:http://www.cnblogs.com/tlxxm/p/4604526.html