1. 程式人生 > >C#基礎-第6章:型別和成員基礎

C#基礎-第6章:型別和成員基礎


本章內容:

  1. 型別的各種成員
  2. 型別的可見性
  3. 成員的可訪問性
  4. 靜態類
  5. 分部類,結構和介面
  6. 元件、多型和版本控制
// #define 可以按版本執行不同的程式碼塊
#define V1
//#define V2a
//#define V2b
//#define V2c

#if !DEBUG
#pragma warning disable 414, 67
#endif
using System;

public sealed class SomeType //P(137)
{                                 //  1
    // 巢狀類
    private
class SomeNestedType { } // 2 // 常量 private const Int32 c_SomeConstant = 1; //readonly 只讀 // 3 private readonly String m_SomeReadOnlyField = "2"; // 4 //靜態可以讀/可寫欄位 private static Int32 s_SomeReadWriteField = 3
; // 5 //型別構造器 static SomeType() { } // 6 // 例項構造器 public SomeType() { } // 7 public SomeType(Int32 x) { } // 8 // 例項方法和靜態方法 public static void Main1() { } //
9 public String InstanceMethod() { return null; } // 10 // 例項屬性 public Int32 SomeProp { // 11 get { return 0; } // 12 set { } // 13 } //例項有參屬性(索引器) public Int32 this[String s] { // 14 get { return 0; } // 15 set { } // 16 } // 例項事件 public event EventHandler SomeEvent; // 17 } internal static class OverridingAccessibility { class Base { //基類虛方法 protected virtual void M() { } } class Derived1 : Base { //重寫基類虛方法 protected override void M() { } } class Derived2 : Base { protected override void M() { } public static void Main1() { } } } //靜態類 staitc關鍵字 永遠不需要例項化 internal static class DifferentCalls //P(146) { public static void Go() { Console.WriteLine(); //呼叫一個靜態方法 Object o = new Object(); o.GetHashCode(); // 呼叫虛例項方法 o.GetType(); // 呼叫非虛例項方法 } } #region Versioning Components With Virtual Methods public sealed class VersioningComponentsWithVirtualMethods { public static void Main() { CompanyB.BetterPhone phone = new CompanyB.BetterPhone(); phone.Dial(); } } /////////////////////////////////////////////////////////////////////// #if V1 namespace CompanyA//P(150) { public class Phone { public void Dial() { Console.WriteLine("Phone.Dial"); // 這裡執行撥號操作 } } } namespace CompanyB { public class BetterPhone : CompanyA.Phone//P(151) { // 加了new 關鍵字 新的Dail方法變得與Phone的Dial方法無關了 new public void Dial() { Console.WriteLine("BetterPhone.Dial"); EstablishConnection(); base.Dial(); } protected virtual void EstablishConnection() { Console.WriteLine("BetterPhone.EstablishConnection"); //在這裡執行建立連線的操作 } } } #endif /////////////////////////////////////////////////////////////////////// #if V2a || V2b || V2c namespace CompanyA { public class Phone { public void Dial() { Console.WriteLine("Phone.Dial"); EstablishConnection(); // Do work to dial the phone here. } protected virtual void EstablishConnection() { Console.WriteLine("Phone.EstablishConnection"); // Do work to establish the connection. } } } #endif /////////////////////////////////////////////////////////////////////// #if V2a namespace CompanyB { public class BetterPhone : CompanyA.Phone { //保留關鍵new,指明該方法與基型別的Dail方法沒有關係 new public void Dial() { Console.WriteLine("BetterPhone.Dial"); EstablishConnection(); base.Dial(); } // 為這個方法新增關鍵new,指明該方法與基型別的 // EstablishConnection 方法沒有關係. new protected virtual void EstablishConnection() { Console.WriteLine("BetterPhone.EstablishConnection"); // 在這裡執行建立連線的操作 } } } #endif #if V2b namespace CompanyB { public class BetterPhone : CompanyA.Phone { //P(153) // 刪除Dail方法 (從基類繼承Dial) // 移除關鍵字'new',將關鍵字Virtual 修改為 override // 指明該方法與基類的EstablishConnection方法的關係 protected override void EstablishConnection() { Console.WriteLine("BetterPhone.EstablishConnection"); // 在這裡執行建立連線的操作 } } } #endif #if V2c namespace CompanyB { public class BetterPhone : CompanyA.Phone { // Keep 'new' to mark this method as having no // relationship to the base type's Dial method. new public void Dial() { Console.WriteLine("BetterPhone.Dial"); EstablishConnection(); base.Dial(); } // Remove 'new' and change 'virtual' to 'override' to // mark this method as having a relationship to the base protected override void EstablishConnection() { Console.WriteLine("BetterPhone.EstablishConnection"); // Do work to establish the connection. } } } #endif #endregion