用接口实现通过改外部文件实现判断调用接口内的方法

来源:互联网 发布:配置linux网络设置的ip 编辑:程序博客网 时间:2024/06/07 03:36
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arrayint = { 1, 2, 4, 5, 6, 2, 8 };
            string[] arraystring = { "a", "zhang", "张三", "asdf", "张海涛", "碟中谍1碟中谍2" };
            ShowMax show = new ShowMax();


            string a= File.ReadAllText(@"c:\Interface.txt");  //通过判断外部文件中的text值来判断调用的方法
            if (a == "int")
            {
                show.maxint(arrayint);
            }
            else
            {
                show.maxstr(arraystring);
            }
            Console.ReadKey();
        }
    }
    
    //定义一个求最大值的接口
    interface IQiuMax
    {
         void  maxstr(string[] arraystring);//返回最长的字符串
         void  maxint(int[] arrayint);   //返回最大的数值
    }
    class ShowMax : IQiuMax
    {
        public void maxstr(string[] arraystring)
        {

            #region 第一种做法:冒泡排序
            //string temp;
            //for (int i = 0; i < arraystring.Length; i++)
            //{
            //    for (int j = i; j < arraystring.Length; j++)
            //    {
            //        if (arraystring[i].Length > arraystring[j].Length)
            //        {
            //            temp = arraystring[i];
            //            arraystring[i] = arraystring[j];
            //            arraystring[j] = temp;
            //        }
            //    }
            //}
            //Console.WriteLine(arraystring[arraystring.Length-1]);
            #endregion
           
            #region 第二种做法:高效做法
                        string max1 = string.Empty;
                        for (int i = 0; i < arraystring.Length; i++)
                        {
                            if (arraystring[i].Length>max1.Length)
                            {
                               max1=arraystring[i];
                            }
                        }
                        Console.WriteLine(max1);
            #endregion

            
        }


        public void maxint(int[] arrayint)
        {
            int temp;
            for (int i = 0; i < arrayint.Length; i++)
            {
                for (int j = i; j < arrayint.Length; j++)
                {
                    if (arrayint[i] > arrayint[j])
                    {
                        temp = arrayint[i];
                        arrayint[i] = arrayint[j];
                        arrayint[j] = temp;
                    }
                }
            }
            Console.WriteLine(arrayint[arrayint.Length - 1].ToString());
            
        }
    }
}

原创粉丝点击