溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

c#接口的問題如何解決

發(fā)布時間:2020-10-10 16:09:11 來源:億速云 閱讀:115 作者:小新 欄目:編程語言

這篇文章主要介紹c#接口的問題如何解決,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

接口的定義就不多說了,它有一個很重要的知識點(diǎn),就是所有繼承這個接口類的都必須實(shí)現(xiàn)接口中的定義,說到這個必須,在團(tuán)隊(duì)開發(fā)中,只要我們商定好了接口,那我們的代碼是不是就統(tǒng)一了!??!

這是我覺得接口重要的第一點(diǎn):它便于我們統(tǒng)一項(xiàng)目的規(guī)定,便于團(tuán)隊(duì)代碼的管理!

再來用一個例子說明:

A公司決定開發(fā)一套動物系統(tǒng),其中包含很多的動物,公司決定要實(shí)現(xiàn)每個動物的喊叫行為……

說到這里,我們一般就是各個程序員拿到自己要實(shí)現(xiàn)的動物類之后就開始大刀闊斧的開干了?。。?/p>

X程序員實(shí)現(xiàn)狗這個類,他寫一個叫喊方法void Han(){……}

Y程序員實(shí)現(xiàn)貓這個類,他寫一個叫喊方法void Shout(){……}

M程序員實(shí)現(xiàn)豬這個類,他寫一個叫喊方法 void Shout(string content){……}

………………

好了,現(xiàn)在都完成了各自需要完成的動物,隔壁老王開始來實(shí)現(xiàn)百獸齊鳴?。。。?amp;¥%¥*%¥¥%¥一頓粗口爆出!這要怎么寫?一個個去調(diào)用???

來看看,X程序員英語不太好,也沒有過多的去管,只是寫出動物叫喊的方法,Y程序員和M程序員寫的叫喊方法名稱是一樣,但M程序員中還要傳遞動物叫喊的內(nèi)容?。。。?!

隔壁老王現(xiàn)在要讓所有動物都叫一遍就得一個動物一個動物的去調(diào)用方法……

OK,接下來開會商量,隔壁老王定義一個動物接口,所有的動物類都得繼承這個接口,這個接口只定義一個void Shout();  (就不過多的寫東西啦,偷偷懶)

X,Y,M程序員繼承后,X,M立馬就發(fā)現(xiàn)有問題,然后開始改了自己手中的類

這時老王就開始來百獸齊鳴啦!哈哈哈哈哈

接下來貼出代碼大家看

接口

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace InterfaceProject
{    /// <summary>
    /// 動物接口    /// </summary>
    interface IAnimal
    {        /// <summary>
        /// 動物叫喊        /// </summary>
        void Shout();
    }
}

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace InterfaceProject
{    /// <summary>
    /// 狗    /// </summary>
    public class Dog:IAnimal
    {        public void Shout()
        {
            Console.WriteLine("汪汪汪");
        }
    }
}

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace InterfaceProject
{    /// <summary>
    /// 貓    /// </summary>
    public class Cat:IAnimal
    {        public void Shout()
        {
            Console.WriteLine("喵喵喵");
        }
    }
}

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace InterfaceProject
{    /// <summary>
    /// 豬    /// </summary>
    public class Pig:IAnimal
    {        public void Shout()
        {
            Console.WriteLine("豬怎么叫來著??豬叫");
        }
    }
}

隔壁老王來實(shí)現(xiàn)百獸齊鳴(打倒老王這種人物的存在)

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace InterfaceProject
{    class Program
    {        static void Main(string[] args)
        {            //百獸齊鳴(這里可以使用反射來初始化所有繼承IAnimal的所有動物,我就不寫這個了,主要看接口)
            List<IAnimal> animals = new List<IAnimal>();
            IAnimal dog = new Dog();
            animals.Add(dog);
            IAnimal cat = new Cat();
            animals.Add(cat);
            IAnimal pig = new Pig();
            animals.Add(pig);            //所有動物都叫一遍
            for (int i = 0; i < animals.Count; i++)
            {
                animals[i].Shout();
            }

            
        }
    }
}

以上是c#接口的問題如何解決的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI