基础才是重中之重~再说面向接口的编程

回到目录

之前在我的文章中有对接口进行过讲解,但感觉讲的还是不够清晰,不够利针见血,这次我把面向接口的编程里,自认为比较核心的两点说一下:

接口详细介绍请看我的这篇文章

基础才是重中之重~为什么C#有显示实现接口

一切依赖于抽象,而不是实现

多个接口相同的行为,被一个对象实现

复制代码
#region 多个接口相同的行为,被一个对象实现(一切依赖于抽象,而不是实现)
    interface IRepository
    {
        void Insert();
    }

    interface IRepositoryAsync
    {
        void Insert();
    }

    class Repository : IRepository
    {

        #region IRepository 成员

        public void Insert()
        {
            Console.WriteLine("同步添加");
        }

        #endregion
    }
    class RepositoryAsync : IRepository, IRepositoryAsync
    {


        #region ICowBoy 成员

        void IRepository.Insert()
        {
            Console.WriteLine("同步添加");
        }

        #endregion

        #region IRepositoryAsync 成员

        void IRepositoryAsync.Insert()
        {
            Console.WriteLine("异步添加");
        }

        #endregion
    }

    #endregion
复制代码

接口实现的多态性

一个接口,多种实现(多态)

复制代码
    #region 一个接口,多种实现(多态)
    interface IHello
    {
        void Morning();
        void Noon();
        void Night();
    }

    class Chinese : IHello
    {

        #region IHello 成员

        public void Morning()
        {
            Console.WriteLine("早上好");
        }

        public void Noon()
        {
            Console.WriteLine("中午好");
        }

        public void Night()
        {
            Console.WriteLine("晚上好");
        }

        #endregion
    }
    class English : IHello
    {
        #region IHello 成员

        public void Morning()
        {
            Console.WriteLine("Good Morning");
        }

        public void Noon()
        {
            Console.WriteLine("Good Noon");
        }

        public void Night()
        {
            Console.WriteLine("Good Night");
        }

        #endregion
    }

    #endregion
复制代码

对于我们开发人员来说,有时,对一个知识的真正理解是需要一个过程,一个时间的,所以建议初学者,应届毕业生同学不用太着急,这个是需要一个过程的,呵呵!

回到目录

posted @   张占岭  阅读(596)  评论(4编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示