zoukankan      html  css  js  c++  java
  • Ninject简单的Demo

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Ninject;
    
    namespace NinjectDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                IKernel kernel = new StandardKernel();
                kernel.Bind<IHuman>().To<Chinese>()              //依赖性链
                    .WithPropertyValue("Capital", "北京")          //指定属性的参数值
                    .WithPropertyValue("SkinColor","黄色")
                    .WithConstructorArgument("count", "13");         //指定构造函数的参数值
    
                //kernel.Bind<IHuman>().To<USA>().WithPropertyValue("Capital", "华盛顿");
                //kernel.Bind<IHuman>().To<Japanese>().WithPropertyValue("Capital", "东京");
    
    
    
                IHuman human = kernel.Get<IHuman>();
                human.Talk();
    
                Console.ReadKey();
            }
        }
    
        //实体类
        public class Person
        {
            public string Name { get; set; }
            public string Language { get; set;}
        }
    
        //接口
    
        public interface IHuman
        {
            void Talk();
        }
    
        //实现接口的类
    
        public class Chinese : IHuman
        {
            public string Capital { get; set; }  //首都
            public string SkinColor { get; set; } //肤色
    
            private string Population { get; set; } //人口
    
            public Chinese(string count)  //构造函数
            {
                Population = count;
            }
    
            public void Talk()
            {
                Console.WriteLine("中国人说,你好!");
                Console.WriteLine("首都:{0}",this.Capital);
                Console.WriteLine("肤色:{0}",this.SkinColor);
                Console.WriteLine("中国共有{0}亿人",this.Population);
            }
        }
    
        public class USA : IHuman
        {
            public string Capital { get; set; }
            public void Talk()
            {
                Console.WriteLine("美国人说,Hello!");
                Console.WriteLine("首都:{0}", this.Capital);
            }
        }
    
        public class Japanese : IHuman
        {
            public string Capital { get; set; }
            public void Talk()
            {
                Console.WriteLine("日本人说,こんにちは!");
                Console.WriteLine("首都:{0}", this.Capital);
            }
        }
    
    
    
    }
  • 相关阅读:
    MySQl查询语句大全
    并发编程三
    并发编程二
    并发编程
    网络编程
    面向对象高级进阶
    python中的面向对象和面向过程
    为什么还需要学习TypeScript
    Chrome 神器,神奇的技巧
    vue-property-decorator知识梳理
  • 原文地址:https://www.cnblogs.com/james641/p/5258771.html
Copyright © 2011-2022 走看看