星空网 > 软件开发 > ASP.net

c#接口深入一步探究其作用,适合新人了解

前言

  前一篇浅显的述说了一下c#接口的作用,并用了一个不怎么好的例子述说了一下。时隔一天,在看完大家的评论后我在论坛中查看了很多前辈们对c#接口的描述,发现大家对例子的说明不是太容易让我们这些新人理解。接下来,就用我这个新手的角度再来阐述一遍。哈哈哈哈……

理论改编故事

  A公司在经过“百兽齐鸣”的事件后汲取了教训,老王也开始用接口这个东西来进行项目的管理。为此,他轻松了不少,沾沾自喜中也就没有去探究接口的其它作用了(没有影射大家,纯属为了编故事)。

  这天,A公司下达了一个新的需求,需要实现由某一个人带着一种动物出去遛弯。老王看到这个需求,轻哼一声:我有接口我怕谁,改接口让小的们去实现相关行为去。

  老王在接口中加了一个 void Walk(string people);下达命令,小的们,赶快实现各个动物中的行为,我要遛弯啦!哈哈哈

  大家匆匆实现好了各自动物的Walk行为后老王就开始来实现某人带动物遛弯的功能了。

  然后他写出了这种代码:

  

c#接口深入一步探究其作用,适合新人了解c#接口深入一步探究其作用,适合新人了解
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace InterfaceProject{  /// <summary>  /// 人  /// </summary>  public class People  {    /// <summary>    /// 姓名    /// </summary>    private string name;    public string Name    {      get { return name; }      set { name = value; }    }    #region 带动物遛弯    public void WalkWithAnimal(Dog dog)    {      dog.Walk(this.name);    }    public void WalkWithAnimal(Cat cat)    {      cat.Walk(this.name);    }    public void WalkWithAnimal(Pig pig)    {      pig.Walk(this.name);    }    #endregion  }}

View Code

  就这样,安稳过了一段时间。突然有一天,A公司来了一批新动物分发到了各个程序员手中,大家快速的完成了各个动物类。

  这时,老王傻眼了。**,这尼玛有1000多个动物,我得写1000个遛弯的东西啊!!!!!天啦!

  老王现在知道了问题所在,立马在Zenkey的博客中查找解决方案,终于,他发现了这篇文章(哈哈哈哈)

  在寻求了解决方法后,他立马改了代码又去得意去了……

c#接口深入一步探究其作用,适合新人了解c#接口深入一步探究其作用,适合新人了解
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace InterfaceProject{  /// <summary>  /// 人  /// </summary>  public class People  {    /// <summary>    /// 姓名    /// </summary>    private string name;    public string Name    {      get { return name; }      set { name = value; }    }    public People(string name)    {      this.Name = name;    }    #region 带动物遛弯    public void WalkWithAnimal(IAnimal animal)    {      animal.Walk(this.name);    }    //public void WalkWithAnimal(Dog dog)    //{    //  dog.Walk(this.name);    //}    //public void WalkWithAnimal(Cat cat)    //{    //  cat.Walk(this.name);    //}    //public void WalkWithAnimal(Pig pig)    //{    //  pig.Walk(this.name);    //}    #endregion  }}

View Code

  得意之余,带所有动物轮流遛弯去!!!

 

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);      //老王王带所有动物都遛一遍      People p = new People("老王");      for (int i = 0; i < animals.Count; i++)      {        p.WalkWithAnimal(animals[i]);      }    }  }}

 

总结

  不知道大家根据这个故事都理解到了什么,这篇文章后我也就不再探究接口的作用了,目前在公司中用接口的这些功能已经让我轻松了不少,希望所有新手都能在我这里将接口这个神物摸索清楚。

  如果有看不明白的请结合上一篇浅谈c#接口来看。

  如果大家赞同请点赞,如果我述说的有不妥的地方希望大家海涵并指出,我们共同进步。

  

 

 

  




原标题:c#接口深入一步探究其作用,适合新人了解

关键词:C#

C#
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流