2017年7月15日 星期六

[C#]interface and abstract class


  • Info

    • General
    • Abstracts
      • 為了實現"多型"精神
    • Interface
      • 介面可以同時"繼承"多個介面

  • General Class

    • UML
    • Code
    ///
    /// General Class
    ///
    public class GeneralClass
    {
        public string Number { get; set; }
        public void ShowNumber()
        {
            System.Console.WriteLine(string.Format("Your number is {0}", Number));
        }
    
        ///
        /// Abstracts method 只能定義在 Abstracts class
        ///
        //public abstract string absName { get; set; }
        //public abstract void absShowName();
    
        public virtual string virYear { get; set; }
        public virtual void virShowYear()
        {
            System.Console.WriteLine(string.Format("You are {0} years old", virYear));
        }
    }
    C#
    • Implementation Code
    ///
    /// Implementation General Class
    ///
    public class GenImplementationClass : GeneralClass
    {
        ///
        /// 1. 在繼承一般類別時,一般的方法不可以被覆寫
        ///
        // public override void ShowNumber() { }
    
        ///
        /// 2. 可以用 new 修飾詞來隱藏父類別中的這個方法
        ///
        public new void ShowNumber()
        {
            System.Console.WriteLine(string.Format("Your house number is {0}", Number));
        }
    
        ///
        /// 3. 父類別中的 virtual mothed 才可以被覆寫
        ///
        public override void virShowYear()
        {
            System.Console.WriteLine(string.Format("This year is {0}", virYear));
        }
    }
    C#

  • Abstracts Class

    • 定義
      • 宣告類別時,使用 abstract 修飾詞
      • abstract 類別只能被繼承,不能直接使用 new 語法建立實體
    • UML
    • Code
    ///
    /// Characteristic
    /// I.Abstracts class
    ///     1.Abstracts class 不能 new 去產生物件
    ///     2.Abstracts class 一定要繼承後,才能使用到它內部的功能
    ///     3.Abstracts class 定義了 Abstracts method ,繼承它的子類別一定要實作這個 method
    ///     4.Abstracts class 沒有要求所有成員都要是抽象的
    ///         但是若 class 中有任何一個 Abstracts 成員,則該 class 必須為 Abstracts class
    /// II.Abstracts method
    ///     1.Abstracts method 只需要宣告, 不需要實作
    ///     1.Abstracts method 只能定義在 Abstracts class
    ///     2.Abstracts method 不能使用 private 存取修飾詞
    ///     3.繼承 Abstracts class 子類別必須實作父類別的 Abstracts method, 否則這子類別還是個 Abstracts class
    ///
    public abstract class AbstractsClass
    {
        ///
        /// general method
        ///
        public string Number { get; set; }
    
        public void ShowNumber()
        {
            System.Console.WriteLine(string.Format("Your number is {0}", Number));
        }
        ///
        /// abstract method: 必須在子類別中被實作
        ///
        public abstract string absName { get; set; }
    
        public abstract void absShowName();
        ///
        /// virtual method: 允許在子類別中被覆寫
        ///
        public virtual string virYear { get; set; }
        public virtual void virShowYear()
        {
            System.Console.WriteLine(string.Format("You are {0} years old", virYear));
        }
    }
    C#
    • Implementation Code
    ///
    /// override: 宣告這個方法是覆寫父類別中的 Abstracts method 或 virtual method
    ///
    public class AbsImplementationClass : AbstractsClass
    {
        ///
        /// 可以用 new 修飾詞來隱藏父類別中的這個方法,
        ///
        public new void ShowNumber()
        {
            System.Console.WriteLine(string.Format("Your house number is {0}", Number));
        }
    
        ///
        /// 繼承 Abstracts class, 一定要實作它的 Abstracts metho, 同樣是以覆寫的方式來做.
        ///         public override string absName { get; set; }
    
        public override void absShowName()
        {
            System.Console.WriteLine(string.Format("Your name is {0}", absName));
        }
    
        ///
        /// override: 覆寫父類別中的 virtual method
        ///
        public override string virYear { get; set; }
    
        public override void virShowYear()
        {
            System.Console.WriteLine(string.Format("This year is {0}", virYear));
        }
    }
    C#

  • Interface Class

    • 定義
      • 可模擬多重繼承,一個類別可同時實作多個介面
      • 使用 inferface 關鍵字宣告,命名習慣最前面加上 I 
      • 僅可定義抽象成員 (不支援 Field,可支援 Property, Method)
        • 不須實作
        • 皆為 public
    • UML
    • Code
    ///
    /// Characteristic
    /// I. Interface
    ///     1.interface 沒有建構方法 (即 method 中沒有參數, 且沒有任何變數的設定)
    ///     2.類別中僅定義 member 名稱,沒有內容
    ///     3.實作這個 Interface method,必需支援該 Interface 的所有 member
    ///     4.介面可包含方法、屬性、事件、索引子,不能包含常數、欄位、運算子、執行個體建構函式
    ///     5.介面裡的 method 必須全部都定義成 public abstract
    ///     6.method 的預設修飾詞就是使用 public abstract ,所以可以省略
    ///         public abstract interface InterfaceClass >>> interface InterfaceClass
    ///
    interface InterfaceClass
    {
        string Number { get; set; }
        void ShowNumber();
        string Name { get; set; }
        void ShowName();
        string Year { get; set; }
        void ShowYear();
    }
    C#
    • Implementation Code
    ///
    /// 當使用類別的繼承功能時,一次只能繼承一個類別,可以當實作介面時,一次可以實作多個介面
    ///
    public class IntImplementationClass : InterfaceClass
    {
        ///
        /// 支援該介面的所有成員
        ///
        public string Number { get; set; }
    
        public void ShowNumber()
        {
            System.Console.WriteLine(string.Format("Your house number is {0}", Number));
        }
    
        public string Name { get; set; }
    
        public void ShowName()
        {
            System.Console.WriteLine(string.Format("Your name is {0}", Name));
        }
    
        public string Year { get; set; }
    
        public void ShowYear()
        {
            System.Console.WriteLine(string.Format("This year is {0}", Year));
        }
    }
    C#
  • Ref:
    • http://jimmu-jimmu.blogspot.tw/2012/10/abstract-classinterface.html
    • http://vito-note.blogspot.tw/2014/08/blog-post_65.html
    • https://docs.microsoft.com/zh-tw/dotnet/csharp/language-reference/keywords/abstract

沒有留言:

張貼留言