zoukankan      html  css  js  c++  java
  • C# 依赖注入????

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ABCDEFG
    {
        public class 依赖注入
        {
            public void test()
            {
                // 功能是 传递不同对象, 执行不同对象下的方法. ( 有隐式转换)
    
                //ServiceClassA serviceA = new ServiceClassA();  //创建 A对象
                //ServiceClassB serviceB = new ServiceClassB();   //创建B对象
                //ClientClass client = new ClientClass(); // 创建 客户端类对象
                //client.Set_ServiceImpl(serviceA);
                //client.ShowInfo();//结果:我是ServceClassA
                //client.Set_ServiceImpl(serviceB);
                //client.ShowInfo();//结果:我是ServceClassB
                //Console.ReadLine();
    
    
                //通过构造函数注入
                ServiceClassA serviceA = new ServiceClassA();  //创建 A对象
                ServiceClassB serviceB = new ServiceClassB();   //创建B对象
                ClientClass client = new ClientClass(serviceA); // 创建 客户端类对象
                client.ShowInfo();//结果:我是ServceClassA
                client = new ClientClass(serviceA);
                client.ShowInfo();//结果:我是ServceClassB
                Console.ReadLine();
            }
        }
    
        //internal    同一程序集内访问    
        //interface   接口
        interface IServiceClass  //定义 服务 接口类
        {
            String ServiceInfo();
        }
        class ServiceClassA : IServiceClass    //A继承接口 ; 实现方法
        {
            public String ServiceInfo()
            {
                return "我是ServceClassA";
            }
        }
        class ServiceClassB : IServiceClass   //B继承接口 ; 实现方法
        {
            public String ServiceInfo()
            {
                return "我是ServceClassB";
            }
        }
    
    
        #region ---Setter注入
        //class ClientClass   //定义 客户 接口类
        //{
        //    //注入点
        //    private IServiceClass _serviceImpl;  //声明接口变量
        //    //客户类中的方法,初始化注入点  
        //    public void Set_ServiceImpl(IServiceClass serviceImpl)
        //    {
        //        this._serviceImpl = serviceImpl;
        //    }
        //    public void ShowInfo()
        //    {
        //        Console.WriteLine(_serviceImpl.ServiceInfo());
        //    }
        //}
        #endregion
    
        #region --构造注入
        class ClientClass
        {
            private IServiceClass _serviceImpl;
    
            public ClientClass(IServiceClass serviceImpl)
            {
                this._serviceImpl = serviceImpl;
            }
    
            public void ShowInfo()
            {
                Console.WriteLine(_serviceImpl.ServiceInfo());
            }
        }
    
        #endregion
    }
  • 相关阅读:
    微信小程序-上传多张图片加进度条(支持预览、删除)
    php中120个内置函数
    angular6 NgModule中定义模块module
    Aliasing input/output properties
    angular6 Can't bind to 'zzst' since it isn't a known property of
    [转]DOM 中 Property 和 Attribute 的区别
    Angular6
    [转]VirtualBox 修改UUID实现虚拟硬盘复制
    pthread_create如何传递两个参数以上的参数
    linux 线程操作问题undefined reference to 'pthread_create'的解决办法(cmake)
  • 原文地址:https://www.cnblogs.com/enych/p/10500157.html
Copyright © 2011-2022 走看看