反射+多态实现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);
}
}
}
相关文章推荐
- servlet 一个simple web project
- oracle backspace 出现^H
- JavaScript对象创建方法
- 说说面向服务的体系架构SOA
- 【Python数据分析】Python3操作Excel(二) 一些问题的解决与优化
- 20135302魏静静——linux课程第六周实验及总结
- Android中Parcelable序列化总结
- BeanUtils的使用-在具有很多相同属性的两个bean之间复制属性值
- [BZOJ3282]Tree(LCT)
- 【02单表查询】——05:CASE表达式
- ArrayList与数组之间的转换
- Hive - 内表和外表的区别
- LeetCode 88 Merge Sorted Array
- AJAX请求乱码
- MySql Host is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts' 解决方法
- Java之JDBC①
- Android系统开发教程之Android开发在虚拟机的优化
- mac 上更改环境变量
- 关于在Android Studio中使用Assets目录下的资源的问题
- java多线程之线程安全的单例模式