面向對象(二)

1.自動屬性

2.接口的實現(正常實現,顯示實現)

3.靜態的初始化與生命週期

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _04自動屬性
{
    abstract class Person // 李村
    {
        private string name;

        // 屬性可讀可寫,沒有約束
        public string Name
        {
            get { return name; }
            set { name = value; }
        }


        // 懶
        public int Age
        {
            get;
            set;
        }
        //未實現(接口裏面的寫法)
        public abstract char Sex
        {
            get;
            set;
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _02接口
{
    //public interface I1
    //{
    //    void Func();
    //}
    //public interface I2
    //{
    //    void Func();
    //}
    //class MyClass : I1, I2
    //{ 
    
    //}

    public interface IMyInterface
    { 
        // 接口裏面可以有的成員:
        // 方法、屬性、索引、事件
        // 是對能力的抽象

        // 成員寫法:1、不需要訪問修飾符;2、沒有方法體
        void Func();
        string Name
        {
            get;
            set;
        }
        string this[int index]
        {
            get;
            set;
        }
        event Action MyEvent;
    }

    // 實現接口:1、正常實現;2、顯式實現
    // 1、就像沒有接口一樣使用
    class MyClass : IMyInterface
    {
        public void Func()
        {
            throw new NotImplementedException();
        }

        public string Name
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string this[int index]
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public event Action MyEvent;
    }
    // 2、顯式實現接口:
    //  爲什麼要有:實現多個接口中有重名的方法
    //  不要修飾符,使用接口名.成員名實現
    class MyClass2 : IMyInterface
    {
        void IMyInterface.Func()
        {
            throw new NotImplementedException();
        }

        string IMyInterface.Name
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        string IMyInterface.this[int index]
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        event Action IMyInterface.MyEvent
        {
            add { throw new NotImplementedException(); }
            remove { throw new NotImplementedException(); }
        }
    }
    //  使用注意:顯式實現的接口對象,只允許使用接口類型進行調用

    public interface I
    {
        void Func();
    }
    class Class : I
    {
        void I.Func()
        {
            throw new NotImplementedException();
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            Class c = new Class();
            I i = c;
            
            i.Func();

            // 接口就是爲了多態而誕生的
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _06靜態的初始化與生命週期以及使用建議
{
    // 靜態在訪問這個類型的第一次進行初始化,在程序結束後纔會被釋放資源
    class MyClass
    {
        // public int numPublic = 10; // 這裏的操作是構造方法做的,是爲了方便程序員給的簡單的語法

        // public static int numStatic = 20;
        public int numPublic;
        public static int numStatic;

        public MyClass()
        {
            numPublic = 10;
        }
        // 靜態構造方法
        static MyClass()
        {
            numStatic = 20;
            // 靜態構造方法目的在初始化靜態成員,不允許手動調用
            // 不允許有重載繼承等內容
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            MyClass m1 = new MyClass();
            // 訪問MyClass->初始化靜態成員(調用靜態構造方法)
            // ->初始化實例成員(調用實例構造方法)


            MyClass m2 = new MyClass();
            // 訪問MyClass->實例構造方法
        }
    }
}


發佈了87 篇原創文章 · 獲贊 6 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章