筆記:CLR var C# 第13章

 1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Text;  5 
 6 namespace ClrVarCSharp.C13Interface  7 {  8     class Program  9  {  10         public static void Main(string[] args)  11  {  12             //Test1();  13             //Test2();  14             //Test3();  15             //Test4();  16             //Test5();  17             //Test6();  18             //A.Test7();  19             //B.Test7();  20             //B.Test8();  21             //Test9();  22             //C.Test10();  23             //D.Test10();  24             //E.Test10();
 25  }  26 
 27         //接口:  28         //對一組方法簽名進行了統一命名  29         //定義了在類型之間進行溝通的標準方式  30 
 31         //13.3 接口定義和實現  32         //定義接口(該定義已包含在FCL中)  33         //public interface IComparable<in T> // in 逆變量,即容許 object -> string  34         //{  35         // int CompareTo(T other);  36         //}  37 
 38         //實現接口
 39         public sealed class Point : IComparable<Point>
 40  {  41             private int m_x, m_y;  42             public Point(int x, int y)  43  {  44                 m_x = x;  45                 m_y = y;  46  }  47             //1). 必須標記爲public  48             //2). 編譯器老是將其標記爲virtual,但派生類不能重寫sealed類的接口方法  49             //3). 但派生類能夠重寫繼承同一接口,併爲接口方法提供本身的實現  50             //IL代碼:.method public final hidebysig newslot virtual
 51             public int CompareTo(Point other)  52  {  53                 return Math.Sign(Math.Sqrt(m_x * m_x + m_y * m_y) -
 54                     Math.Sqrt(other.m_x * other.m_x + other.m_y * other.m_y));  55  }  56  }  57 
 58         //13.5 顯示/隱式實現接口
 59 
 60         internal sealed class SimpleType : IDisposable  61  {  62             //隱式實現接口
 63             public void Dispose()  64  {  65                 Console.WriteLine("Public Dispose");  66  }  67             //顯示實現接口,不能定義訪問修飾符,老是private  68             //IL代碼:.method private final hidebysig newslot virtual
 69             void IDisposable.Dispose()  70  {  71                 Console.WriteLine("IDisposable Dispose");  72  }  73  }  74         static void Test1()  75  {  76             SimpleType st = new SimpleType();  77  st.Dispose();  78             IDisposable d = st;  79             d.Dispose();  //若爲顯示實現接口,結果爲 Public Dispose
 80  }  81 
 82         //13.6 泛型接口  83 
 84         //非泛型接口
 85         static void Test2()  86  {  87             int x = 1, y = 2;  88             IComparable c = x;  89             c.CompareTo(y);  //發生裝箱  90             //c.CompareTo("2"); //運行時異常
 91  }  92         //泛型接口
 93         static void Test3()  94  {  95             int x = 1, y = 2;  96             IComparable<int> c = x;  97             c.CompareTo(y);  //不發生裝箱  98             //c.CompareTo("2"); //編譯錯誤
 99  } 100 
101         //實現同一個接口的多個泛型版本
102         public sealed class Number : IComparable<int>, IComparable<string>
103  { 104             private int m_val = 5; 105             public int CompareTo(int other) 106  { 107                 return m_val.CompareTo(other); 108  } 109             public int CompareTo(string other) 110  { 111                 return m_val.CompareTo(other); 112  } 113  } 114 
115         //13.7 約束方法的參數類型爲實現指定接口的類型
116         static void Test4() 117  { 118             int x = 5; 119             Guid g = new Guid(); 120             M(x);  //int 同時實現了 IComparable 和 IConvertible 121             //M(g); //編譯錯誤,guid沒有實現IConvertible
122  } 123         static int M<T>(T t) where T : IComparable, IConvertible 124  { 125             return 0; 126  } 127 
128         //13.8 一個類實現不一樣接口的重簽名方法 129         //1). 定義接口
130         public interface IWindow 131  { 132             object GetMenu(); 133  } 134         public interface IRestaurant 135  { 136             object GetMenu(); 137  } 138         //2). 隱式實現接口
139         public sealed class MarioPizzeria : IWindow, IRestaurant 140  { 141             public object GetMenu()   //書中「必須使用顯式接口方法實現」說法是錯誤的,可同時實現兩個接口!
142  { 143                 Console.WriteLine("0"); 144                 return "0"; 145  } 146  } 147         static void Test5() 148  { 149             MarioPizzeria mp = new MarioPizzeria(); 150  mp.GetMenu(); 151             IWindow window = mp; 152  window.GetMenu(); 153             IRestaurant restaurant = mp; 154  restaurant.GetMenu(); 155  } 156         //3). 顯式實現接口
157         public sealed class MarioPizzeria2 : IWindow, IRestaurant 158  { 159             public object GetMenu() 160  { 161                 Console.WriteLine("0"); 162                 return "0"; 163  } 164             object IWindow.GetMenu() 165  { 166                 Console.WriteLine("1"); 167                 return "1"; 168  } 169             object IRestaurant.GetMenu() 170  { 171                 Console.WriteLine("2"); 172                 return "2"; 173  } 174  } 175         static void Test6() 176  { 177             MarioPizzeria2 mp = new MarioPizzeria2(); 178             mp.GetMenu();   //GetMenu()只是該類的一個普通方法,若未實現則出現編譯錯誤
179             IWindow window = mp; 180  window.GetMenu(); 181             IRestaurant restaurant = mp; 182  restaurant.GetMenu(); 183  } 184 
185         //13.9 用顯式接口方法實現來加強編譯時類型安全 186         //原理是顯式實現接口只能「顯式」調用 187         //1). 隱式實現接口可能發生裝箱和運行時異常
188         class A 189  { 190             public interface IComparable1 191  { 192                 int CompareTo(object other); 193  } 194             struct SomeValueType : IComparable1 195  { 196                 private int m_x; 197                 public SomeValueType(int x) { m_x = x; } 198                 public int CompareTo(object other) 199  { 200                     return m_x - ((SomeValueType)other).m_x; 201  } 202  } 203             public static void Test7() 204  { 205                 SomeValueType v = new SomeValueType(0); 206                 object o = new object(); 207                 int n = v.CompareTo(v);   //裝箱 SomeValueType -> Object 208                 //n = v.CompareTo(o); //運行時異常
209  } 210  } 211         //2). 顯式實現接口不發生裝箱和編譯時類型安全
212         class B 213  { 214             public interface IComparable1 215  { 216                 int CompareTo(object other); 217  } 218             struct SomeValueType : IComparable1 219  { 220                 private int m_x; 221                 public SomeValueType(int x) { m_x = x; } 222                 public int CompareTo(SomeValueType other) 223  { 224                     return m_x - other.m_x; 225  } 226                 int IComparable1.CompareTo(object other) 227  { 228                     return CompareTo((SomeValueType)other); 229  } 230  } 231             public static void Test7() 232  { 233                 SomeValueType v = new SomeValueType(0); 234                 object o = new object(); 235                 int n = v.CompareTo(v);   //不發生裝箱 236                 //n = v.CompareTo(o); //編譯錯誤,由於顯式接口只能顯式調用
237  } 238             //3). 從新發生裝箱和運行時異常
239             public static void Test8() 240  { 241                 SomeValueType v = new SomeValueType(0); 242                 IComparable1 c = v;  //裝箱 struct -> 引用類型
243                 object o = new object(); 244                 int n = c.CompareTo(v);  //裝箱 引用類型 -> object 245                 //n = c.CompareTo(o); //編譯異常
246  } 247  } 248 
249         //13.10 顯式實現接口缺陷
250         public static void Test9() 251  { 252             int x = 5; 253             //1). In32顯式實現了IConvertible接口,但此處沒有相關幫助 254             //Single s = x.ToSingle(null); 255             //2). Int32值型轉換爲IConvertible發生裝箱
256             Single s = ((IConvertible)x).ToSingle(null); 257  } 258         class C 259  { 260             public static void Test10() 261  { 262                 var d1 = new Derived(); 263                 var d2 = new Derived(); 264  d1.CompareTo(d2); 265  } 266             internal class Base : IComparable 267  { 268                 int IComparable.CompareTo(object obj) 269  { 270                     Console.WriteLine("Base's CompareTo"); 271                     return 0; 272  } 273  } 274             internal sealed class Derived : Base, IComparable 275  { 276                 public int CompareTo(object o) 277  { 278                     Console.WriteLine("Derived's CompareTo"); 279 
280                     //沒有CompareTo方法 281                     //base.CompareTo(o); 282 
283                     //IComparable c = this; //this實現了IComparable接口 284                     //c.CompareTo(o); //因此此處在調用自身致使無窮遞歸
285 
286                     return 0; 287  } 288  } 289  } 290         class D 291  { 292             public static void Test10() 293  { 294                 var d1 = new Derived(); 295                 var d2 = new Derived(); 296  d1.CompareTo(d2); 297  } 298             internal class Base : IComparable 299  { 300                 int IComparable.CompareTo(object obj) 301  { 302                     Console.WriteLine("Base's CompareTo"); 303                     return 0; 304  } 305  } 306             internal sealed class Derived : Base/*, IComparable*/
307  { 308                 public int CompareTo(object o) 309  { 310                     Console.WriteLine("Derived's CompareTo"); 311 
312                     //沒有CompareTo方法 313                     //base.CompareTo(o); 314 
315                     //調用顯式接口方法
316                     IComparable c = this; 317  c.CompareTo(o); 318 
319                     return 0; 320  } 321  } 322  } 323         class E 324  { 325             public static void Test10() 326  { 327                 var d1 = new Derived(); 328                 var d2 = new Derived(); 329  d1.CompareTo(d2); 330  } 331             internal class Base : IComparable 332  { 333                 int IComparable.CompareTo(object obj) 334  { 335                     Console.WriteLine("Base's CompareTo"); 336                     return 0; 337  } 338                 public virtual int CompareTo(object o) 339  { 340                     Console.WriteLine("Base's virtual CompareTo"); 341                     return 0; 342  } 343  } 344             internal sealed class Derived : Base, IComparable 345  { 346                 public override int CompareTo(object o) 347  { 348                     Console.WriteLine("Derived's CompareTo"); 349                     //此處調用的是虛方法,而非接口的顯式實現方法
350                     return base.CompareTo(o); 351  } 352  } 353  } 354 
355  } 356 }
相關文章
相關標籤/搜索