抽象类、接口、事件、Action、Func

抽象类负责对类的抽象,经常使用在Template等模式中

接口负责对类的行为的抽象

事件负责对两个对象的通信,而不需要建立两个对象直接联系,避免了对象的间的耦合

Action与Func在基于策略模式实现弱依赖关系,即Test(IStrategy strategy);实现对抽象方法取代真实方法的调用。


抽象类实现

    public abstract class Test
    {
        public virtual int Add(int x,int y)
        {
            return x + y;
        }

        public abstract int Muliti(int x, int y);
    }



接口实现

    public interface IOperation
    {
        void Logger(string message);
    }

    public abstract class Test:IOperation
    {
        public virtual int Add(int x,int y)
        {
            return x + y;
        }

        public abstract int Muliti(int x, int y);

        public void Logger(string message)
        {
            throw new NotImplementedException();
        }
    }

事件实现

    public interface IOperation
    {
        void Logger(string message);
    }

    public abstract class Test:IOperation
    {
        public event Action OnRefresh;

        public virtual int Add(int x,int y)
        {
            if (OnRefresh != null)
            {
                Action action = OnRefresh;
                action();
            }
            return x + y;
        }

        public abstract int Muliti(int x, int y);

        public void Logger(string message)
        {
            throw new NotImplementedException();
        }
    }

Action与Func实现对常见异常代码的封装和调用

    class _09exception
    {
        public static void Main1()
        {
            BussinessHelper.Action(() =>
            {
                throw new MyException("aa", "bb");
            });
        }
    }

    class BussinessHelper
    {
        public static void Action(Action action)
        {
            try
            {
                if (action != null)
                    action();
            }
            catch (MyException ex)
            {

            }
        }
    }

    class MyException : Exception
    {
        public MyException(string message)
            : base(message)
        { 
            
        }

        public MyException(string message, string title)
            :base(message)
        {
            Console.WriteLine(message);
        }

    }




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值