您的位置:首页 > 其它

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

2012-06-05 20:13 941 查看
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());

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐