您的位置:首页 > 其它

反射+多态实现0判断更具不同类型排序

2016-03-28 16:55 471 查看
   

   我们先看看问题描述:

   


   实现效果:

   


     一:按照判断的方式来做

     

    1:先实现排序接口

      public class NameCom : IComparer<Product>
{
public int Compare(Product x, Product y)
{
IComparer<Product> pp = new NameCom();
return x.Name.CompareTo(y.Name);
}
}

public class PriceCom : IComparer<Product>
{
public int Compare(Product x, Product y)
{
return x.Price.CompareTo(y.Price);
}
}

public class NumberCom:IComparer<Product>
{
public int Compare(Product x, Product y)
{
return x.Number.CompareTo(y.Number);
}
}

public class BrandCom : IComparer<Product>
{
public int Compare(Product x, Product y)
{
return x.Brand.CompareTo(y.Brand);
}
}

public class SellCom : IComparer<Product>
{
public int Compare(Product x, Product y)
{
return x.Sell.CompareTo(y.Sell);
}
}
     2:定义产品类

       public class Product
{
public string Name { get; set; }
public string Number { get; set; }
public int Price { get; set; }
public string Brand { get; set; }
public int Sell { get; set; }
public int Repository { get; set; }
}
     3:具体实现

     class Program
{
static void Main(string[] args)
{
List<Product> plist = new List<Product>();
plist.Add(new Product() { Name = "冰箱", Number = "B213", Price = 2300, Brand = "西门子", Sell = 100, Repository = 230 });
plist.Add(new Product() { Name = "彩电", Number = "C3145", Price = 1800, Brand = "长虹", Sell = 32, Repository = 18 });
plist.Add(new Product() { Name = "洗衣机", Number = "X888", Price = 2119, Brand = "海尔", Sell = 1000, Repository = 88 });
plist.Add(new Product() { Name = "微波炉", Number = "W337", Price = 998, Brand = "美的", Sell = 3568, Repository = 10000 });
plist.Add(new Product() { Name = "热水器", Number = "Y66", Price = 1080, Brand = "华帝", Sell = 1323, Repository = 3600 });

Console.WriteLine("原序输出:");
Show(plist);

while (true)
{
string str = Console.ReadLine();
IComparer<Product> ic=null;

if (str == "A")
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("根据名称的降序显示:");
ic = new NameCom();
plist.Sort(ic);
plist.Reverse();
}
if (str == "B")
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("根据编号的升序显示:");
ic = new NameCom();
plist.Sort(ic);
}
if (str == "C")
{
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("根据价格的降序显示:");
ic = new PriceCom();
plist.Sort(ic);
plist.Reverse();
}
if (str == "D")
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("根据品牌的升序显示:");
ic = new BrandCom();
plist.Sort(ic);
}
if (str == "E")
{
Console.ForegroundColor = ConsoleColor.DarkCyan;
Console.WriteLine("根据销售数量的降序显示:");
ic = new BrandCom();
plist.Sort(ic);
}
if (str == "G")
{
break;
}

if (ic == null)
{
Console.WriteLine("无法进行排序→_→");
}
else
{
Show(plist);
}
}

}

public static void ShowTitle()
{
Console.WriteLine("商品名称\t商品编号\t销售价格\t商品品牌\t销售数量");
}

public static void Show(List<Product> plist)
{
ShowTitle();
foreach (Product item in plist)
{
Console.WriteLine("{0,-10}\t{1,-10}\t{2,-10}\t{3,-10}\t{4,-10}", item.Name, item.Number, item.Price, item.Brand, item.Sell);
}
}
}       这种每种类型都要判断的方法,多了就会很不好

     一:反射+多态的方式来实现

           类和排序接口实现一样

    1:定义抽象类或接口提供一个sort方法,不同排序方法实现该接口或抽象类

     public abstract class NoCheck
{
public abstract void Sort(List<Product> plist);
}

public class NoCheckA : NoCheck
{
public override void Sort(List<Product> plist)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("根据名称的降序显示:");
NameCom ic = new NameCom();
plist.Sort(ic);
plist.Reverse();
}
}

public class NoCheckB : NoCheck
{
public override void Sort(List<Product> plist)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("根据编号的升序显示:");
NameCom ic = new NameCom();
plist.Sort(ic);
}
}
public class NoCheckC : NoCheck
{
public override void Sort(List<Product> plist)
{
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("根据价格的降序显示:");
PriceCom ic = new PriceCom();
plist.Sort(ic);
plist.Reverse();
}
}

    2:使用

     public class Test
{
public static void Main()
{
List<Product> plist = new List<Product>();
plist.Add(new Product() { Name = "冰箱", Number = "B213", Price = 2300, Brand = "西门子", Sell = 100, Repository = 230 });
plist.Add(new Product() { Name = "彩电", Number = "C3145", Price = 1800, Brand = "长虹", Sell = 32, Repository = 18 });
plist.Add(new Product() { Name = "洗衣机", Number = "X888", Price = 2119, Brand = "海尔", Sell = 1000, Repository = 88 });
plist.Add(new Product() { Name = "微波炉", Number = " W337", Price = 998, Brand = "美的", Sell = 3568, Repository = 10000 });
plist.Add(new Product() { Name = "热水器", Number = "Y66", Price = 1080, Brand = "华帝", Sell = 1323, Repository = 3600 });

Console.WriteLine("原序输出:");
Show(plist);

while (true)
{
try
{
string str = Console.ReadLine();
//反射得到具体的类在调用方法
NoCheck nc = Activator.CreateInstance(Type.GetType("Lesson7.NoCheck" + str)) as NoCheck;
nc.Sort(plist);
Show(plist);
}
catch (Exception e)
{
Console.WriteLine("无法进行排序......");
}
}

}

public static void ShowTitle()
{
Console.WriteLine("商品名称\t商品编号\t销售价格\t商品品牌\t销售数量");
}

public static void Show(List<Product> plist)
{
ShowTitle();
foreach (Product item in plist)
{
Console.WriteLine("{0,-10}\t{1,-10}\t{2,-10}\t{3,-10}\t{4,-10}", item.Name, item.Number, item.Price, item.Brand, item.Sell);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: