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);
            }
        }
    
    
    
    }
  • 相关阅读:
    SDUT 1570 C 旅行(DFS)
    SDUT 1269 走迷宫(BFS)
    求连通分量个数
    Codeforces Round #237 (Div. 2)
    FZU 2150 Fire Game(BFS)
    HDU 2045 不容易系列之(3)—— LELE的RPG难题(递推)
    LeetCode 155. Min Stack
    【ZZ】终于有人把云计算、大数据和人工智能讲明白了!
    学习笔记之Microsoft Office 365
    【ZZ】技能表合集
  • 原文地址:https://www.cnblogs.com/james641/p/5258771.html
Copyright © 2011-2022 走看看