首页 > 其他 > 详细

接口-配置文件

时间:2019-12-07 21:47:28      阅读:126      评论:0      收藏:0      [点我收藏+]

实现功能:通过更改配置文件实现不同的功能

1,创建以下内容

技术分享图片

 

 2,ICar接口代码

技术分享图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 接口配置文件
{
  public  interface ICar
    {
        void wheel();
        void Light();
    }
}
View Code

3,Car代码

技术分享图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 接口配置文件
{
    public class Car : ICar
    {
        public void Light()
        {
            Console.WriteLine("我有8个灯");
        }

        public void wheel()
        {
            Console.WriteLine("我有四个轮子");
        }
    }
}
View Code

4,Bike代码

技术分享图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 接口配置文件
{
    public class Bike : ICar
    {
        public void Light()
        {
            Console.WriteLine("我有一个灯");
        }

        public void wheel()
        {
            Console.WriteLine("我有两个轮子");
        }
    }
}
View Code

5,添加引用

技术分享图片

 

6,编写配置文件

技术分享图片
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
  
  //下面这一部分
  <appSettings>
    <add key="Icar" value="Bike"/>  
  </appSettings>
  
  
</configuration>
View Code

 

 7,Factory代码

一定要 using System.Configuration;

技术分享图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
namespace 接口配置文件
{
   public  class Factory
    {
        private static string ICar = ConfigurationManager.AppSettings["Icar"];
        public static ICar ObjectFactory()
        {
            if (ICar=="Car")
            {
                return new Car();
            }
            else 
            {
                return new Bike();
            }
               
        }

    }
}
View Code

 

8,程序调用

技术分享图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 接口配置文件
{
    class Program
    {
        static void Main(string[] args)
        {
            ICar car = Factory.ObjectFactory();
            car.Light();
            car.wheel();
            Console.ReadLine();
        }
    }
}
View Code

9,更该一些内容即可

技术分享图片

 

 技术分享图片

 

 

 

 

 

 

 

 

接口-配置文件

原文:https://www.cnblogs.com/Luck1996/p/12003401.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!