學習日誌-abstract class and interface

來源:互聯網
上載者:User

學一點記一點,免得忘記..

 

看了很多前輩寫的關於abstract class and interface的文章,自己也試著去梳理了一遍。

 

abstract class :

1.抽象類別不能執行個體化。

2.抽象類別可以有構造方法,可以在其衍生類別中調用。

3.抽象類別中的抽象方法(沒有方法體)在衍生類別中必須重寫,非抽象方法(有方法體)可以不重寫。

4.抽象類別可以有欄位,屬性,方法,建構函式等成員。
5.抽象類別對於介面只能滿足單一繼承。

 

 

代碼

    abstract class Myabstract
    {
        public Myabstract()     {       Console.WriteLine("Abstract class constructors");      }
        public abstract void abstractMethod();
        public void Method()
        {
            Console.WriteLine("Myabstract Method");
        }
    }

 

 

interface:

1.介面不能執行個體化。

2.介面不可以有構造方法。

3.介面中的方法不需要方法體,在衍生類別中必須重寫方法。

4.介面不可以有欄位。
5.介面的方法不需要修飾符,其含義類似public。

6.介面可以滿足“多繼承”。

 

 

    interface MyInterface
    {
        void interfaceMethod();
    }

 

 

 Class:

 

代碼

class MyClass : Myabstract,MyInterface
    {
        public MyClass()
            : base()
        {
        }

        public override void abstractMethod()
        {
            Console.WriteLine("MyClass Override Abstract Method");
        }

        public void interfaceMethod()
        {
            Console.WriteLine("MyClass Override Interface Method");
        }
    }

 

 

Main Method:

 

代碼

    class Program
    {
        static void Main(string[] args)
        {
            MyClass myclass = new MyClass();
            myclass.abstractMethod();
            myclass.interfaceMethod();
            myclass.Method();
        }
    }

 

 

輸出:

Abstract Class Constructors

MyClass Override Abstract Method

MyClass Override Interface Method

Myabstract Method

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.