C#??????滻switch?????????д??
???????????? ???????[ 2015/7/17 13:27:16 ] ??????????????????? ????????
????????????????????switch????ж???????????????????????????м?????????????????case?ж??????и??????д?????switch??????
??????????????????????????????????????????????????????????????????????????????????switch??????ж????????м?????????????????????????switch??????????
??????????????????
????public interface IValueProcessor
????{
????decimal DaZhe(short policy??decimal orginPrice);
????}
?????β?policy?????????й???????????β?orginPrice?????????????й?????????????
????public enum PolicyEnum
????{
????Wuzhe = 0??
????LiuZhe = 1??
????QiZhe =2??
????BaZhe =3??
????JiuZhe = 4
????}
???????IValueProcessor????????????PolicyEnum?????????????
public class MyValueProcessor : IValueProcessor
{
public decimal DaZhe(short policy??decimal orginPrice)
{
switch (policy)
{
case (short)PolicyEnum.Wuzhe:
return orginPrice / 2;
case (short)PolicyEnum.LiuZhe:
return orginPrice * (decimal)0.6;
case (short)PolicyEnum.QiZhe:
return orginPrice * (decimal)0.7;
case (short)PolicyEnum.BaZhe:
return orginPrice * (decimal)0.8;
case (short)PolicyEnum.JiuZhe:
return orginPrice * (decimal)0.9;
default:
return orginPrice / 2;
}
}
}
???????????????£?
????static void Main(string[] args)
????{
????Console.WriteLine("??????????????0???5???1???6???2???7???3???8???4???9???");
????string policy = Console.ReadLine();
????decimal originPrice = (decimal)100.00;
????Console.WriteLine("????????????"+ originPrice);
????MyValueProcessor processor = new MyValueProcessor();
????Console.WriteLine("???????????"+ processor.DaZhe(short.Parse(policy)??originPrice));
????Console.ReadKey();
????}
????????д???????????????????滻switch?ж???????????д?????
??????MyValueProcessor???DaZhe(short policy??decimal orginPrice)?????У????????short??????βκ????decimal??????βΣ?????decimal?????????????????short??????β????switch?????ж???????????ò??????????????????????????????short?????????伯???е?key??????????????????伯???value????????????????????????????????伯???С???????£?
public class MyValueProcessor : IValueProcessor
{
private readonly Dictionary<short?? Func<decimal?? decimal>> _dic;
public MyValueProcessor()
{
_dic = new Dictionary<short?? Func<decimal?? decimal>>
{
{0?? m => m * (decimal)0.5}??
{1?? m => m * (decimal)0.6}??
{2?? m => m * (decimal)0.7}??
{3?? m => m * (decimal)0.8}??
{4?? m => m * (decimal)0.9}
};
}
public decimal DaZhe(short policy??decimal orginPrice)
{
if (_dic.ContainsKey(policy))
{
return _dic[policy].Invoke(orginPrice);
}
return orginPrice / 2;
}
}
????????????DaZhe(short policy??decimal orginPrice)????????????ж?????short??????????????伯???key???????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11