c#接口


1.什么是接口

接口是指定一組函數成員而不實現他們的引用類型,

class CA
{
        public string name;
        public int age;
}
class CB
{
        public string First;
        public string Last;
        public double PersonsAge;
}
 class Program
    {
        static void Main(string[] args)
        {
            CA a = new CA()
                {
                    name = "holliszzz",
                    age = 25
                };
            PrintInfo(a);
            Console.ReadKey();
        }

        static void PrintInfo(CA item)
        {
            Console.WriteLine("Name {0} , Age {1}", item.name, item.age);
        }


        //只有傳入的是CA對象才會正常工作,傳入CB對象就不行;
        //因為PrintInfo形參指明實參必須為CA類型,且CB與CA結構不同,
    }

解決辦法,

 interface Info// 聲明接口
{
        string GetName();
        string GetAge();
}
class CA : Info
{
        public string name;
        public int age;
        public string GetName() //實現兩個接口方法
        {
            return name;
        }
        public string GetAge()
        {
            return age.ToString();
        }
}
class CB : Info
{
        public string First;
        public string Last;
        public double PersonsAge;
        public string GetName()// 實現兩個接口方法
        {
            return First + "" + Last;
        }
        public string GetAge()
        {
            return PersonsAge.ToString();
        }
}
class Program
    {
        static void Main(string[] args)
        {
            CA a = new CA()
                {
                    name = "張三",
                    age = 25
                };
            CB b = new CB()
                {
                    First = "",
                    Last = "",
                    PersonsAge = 26
                };
            PrintInfo(a);
            PrintInfo(b);
            Console.ReadKey();

        }
        static void PrintInfo(Info item)// 傳入接口引用
        {
            Console.WriteLine("name = {0} , age = {1}", item.GetName(), item.GetAge());
        }
    }

2.icomparable_接口示例

 class MyClass : IComparable// 類實現引用
{
        public int TheValue;
        public int CompareTo(object obj)// 實現方法
        {
            MyClass mc = (MyClass)obj;
            if (this.TheValue < mc.TheValue) return -1;
            if (this.TheValue > mc.TheValue) return 1;
            return 0;
        }
}
 class Program
    {
        static void Main(string[] args)
        {
            //var MyInt = new[] { 1, 3, 2, 4, 5 };
            //Array.Sort(MyInt);
            //foreach (var item in MyInt)
            //{
            //    Console.Write("{0}", item);
            //}
            //Console.ReadKey();

            var myInt = new[] { 1, 4, 7, 9, 2 };
            MyClass[] mcArr = new MyClass[5];
            for (int i = 0; i < 5; i++)
            {
                mcArr[i] = new MyClass();
                mcArr[i].TheValue = myInt[i];
            }
            PrintOut("初始化數組{0}", mcArr);
            Array.Sort(mcArr);
            PrintOut("排序后數組{1}", mcArr);
            Console.ReadKey();

        }

        static void PrintOut(string s, MyClass[] mc)
        {
            Console.Write(s);
            foreach (var item in mc)
            {
                Console.Write("{0}", item.TheValue);
            }
            Console.WriteLine("");
        }
    }

3.接口聲明

static void Main(string[] args)
        {
            //接口聲明不能包含以下成員:數據成員,靜態成員,
            //接口聲明只能包含以下非靜態成員:方法,屬性,事件,索引器,
            //函數成員聲明不能包含任何代碼實現,且實用分號結尾,
            //接口聲明可以有任何的訪問修飾符,接口成員是隱式的public,不允許寫任何訪問修飾符包括public,
        }

4.接口實現

interface Info// 聲明接口
    {
        void PrintOut(string s);
    }
 class MyClass : Info// 實現接口
    {
        public void PrintOut(string s)
        {
            Console.WriteLine("簡單接口調用示例{0}", s);
        }
    }
 class Program
    {
        static void Main(string[] args)
        {
            MyClass mc = new MyClass();// 創建實例調用,
            mc.PrintOut("Test");
            Console.ReadKey();

            //如果類實現了接口,就必須實現接口的所有成員,
            //如果類從基類繼承並實現接口,基類名稱必須放在所有接口名稱之前,
        }
    }

5.接口是引用類型 

 class Program
    {
        static void Main(string[] args)
        {
            MyClass mc = new MyClass();
            mc.PrintOut("Object");

            Info inf = (Info)mc;    // 將類對象的引用轉換為接口類型的引用,
            //Info i = mc as Info;    //as 運算符的使用
            inf.PrintOut("Interface");

            Console.ReadKey();
        }
    }

    interface Info
    {
        void PrintOut(string s);
    }

    class MyClass : Info
    {
        public void PrintOut(string s)
        {
            Console.WriteLine("Calling through {0}", s);
        }
    }

6.派生成員作為實現

 class Program
    {
        static void Main(string[] args)
        {
            //實現接口的類可以從它的基類繼承實現的代碼,

            Derived d = new Derived();
            d.PrintOut("yes");
            Console.ReadKey();
        }
    }

    interface info
    {
        void PrintOut(string s);
    }

    class MyBaseClass
    {
        public void PrintOut(string s)
        {
            Console.WriteLine("不實現接口 {0}", s);
        }
    }

    class Derived : MyBaseClass, info
    {

    }

7.顯示成員接口實現

class Program
    {
        static void Main(string[] args)
        {
            //單個類可以實現多個接口需要的所有成員,
            //我們希望每一個接口分離實現該怎么做,即創建顯示接口成員實現,


            MyClass mc = new MyClass(); // 創建類對象
            Info1 i1 = (Info1)mc;       //獲取Info1的引用
            i1.PrintOut("Interface1");  //調用顯示實現
            Info2 i2 = (Info2)mc;
            i2.PrintOut("Interface2");
            Console.ReadKey();
        }
    }

    interface Info1 // 聲明接口
    {
        void PrintOut(string s);
    }

    interface Info2
    {
        void PrintOut(string s);
    }

    class MyClass : Info1, Info2
    {
        void Info1.PrintOut(string s) // 限定接口名
        {
            Console.WriteLine("Info1 {0}", s);
        }

        void Info2.PrintOut(string s)
        {
            Console.WriteLine("Info2 {0}", s);
        }
    }
    class MyClass : Info1
    {
        //顯示接口成員實現只可以通過指向接口的引用來訪問,
        //即使是類中的另一成員也不可以直接訪問顯示實現,

        void Info1.PrintOut(string s)
        {
            Console.WriteLine("Info1");
        }

        public void Test()
        {
            ((Info1)this).PrintOut("successful");//***
        }

        //強制轉換當前對象的引用(this)為接口類型的引用,並使用指向接口的引用來調用顯示接口實現,
        //其其它類成員不能直接訪問顯示接口成員實現,他們必須通過接口的引用來訪問,
    }

8.接口可以繼承接口

 class Program
    {
        static void Main(string[] args)
        {
            MyData md = new MyData();
            md.SetData(5);
            Console.WriteLine("value = {0}", md.GetData());
            Console.ReadKey();
        }
    }

    interface IDataRetrieve
    {
        int GetData();
    }

    interface IDataStore
    {
        void SetData(int x);
    }

    interface IDataIO : IDataRetrieve, IDataStore
    {

    }

    class MyData : IDataIO
    {
        int x;
        public int GetData()
        {
            return x;
        }
        public void SetData(int n)
        {
            x = n;
        }
    }

9.不同類實現一個接口示例

class Program
    {
        static void Main(string[] args)
        {
            Animal[] a = new Animal[3]; //創建Animal數組,
            a[0] = new Cat();           
            a[1] = new Dog();           
            a[2] = new Bird();
            foreach (Animal item in a) //var = Animal 在數組a中循環,
            {
                ILiveBirth ilb = item as ILiveBirth;
                if (ilb != null)
                {
                    Console.WriteLine("Baby is called {0}", ilb.BabyCalled());
                }
            }
            Console.ReadKey();
        }
    }

    interface ILiveBirth //聲明接口
    {
        string BabyCalled();
    }

    class Animal //聲明基類
    {

    }

    class Cat : Animal, ILiveBirth //聲明Cat類
    {
        string ILiveBirth.BabyCalled()
        {
            return "miao";
        }
    }

    class Dog : Animal, ILiveBirth //聲明Dog類
    {
        string ILiveBirth.BabyCalled()  // ILiveBirth手動敲打的,
        {
            return "wang";
        }
    }

    class Bird : Animal //聲明Bird類
    {

    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM