国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#可以減少或不使用switch有什么方法

2024-09-07 17:05:33
字體:
來源:轉載
供稿:網友
Insus.NET的解決方法,是使用工廠方法來處理,可以創建一個工廠接口,然后每個方法設計為一個工廠類,并實現工廠接口。
工廠接口:
代碼如下:
IGetFactory
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for IGetFactory
/// </summary>
namespace Insus.NET
{
public interface IGetFactory
{
string GetResult();
}
}

Get工廠類:
代碼如下:
GetFactory
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for GetFactory
/// </summary>
namespace Insus.NET
{
public class GetFactory : IGetFactory
{
public GetFactory()
{
//
// TODO: Add constructor logic here
//
}
public string GetResult()
{
return "get";
}
}
}

GetTest類:
代碼如下:
GetTestFactory
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for GetTestFactory
/// </summary>
namespace Insus.NET
{
public class GetTestFactory : IGetFactory
{
public GetTestFactory()
{
//
// TODO: Add constructor logic here
//
}
public string GetResult()
{
return "gettest";
}
}
}

以及GetSet類:
代碼如下:
GetSetFactory
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for GetSetFactory
/// </summary>
namespace Insus.NET
{
public class GetSetFactory : IGetFactory
{
public GetSetFactory()
{
//
// TODO: Add constructor logic here
//
}
public string GetResult()
{
return "getset";
}
}
}

因此你的代碼最終變為:
代碼如下:
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public string Exec(string mothedName)
{
string ret = "";
//switch (mothedName)
//{
// case "get":
// ret = get();
// break;
// case "get1":
// ret = gettest();
// break;
// //.....
// case "testget":
// ret = getrset();
// break;
//}
IGetFactory get = new GetTestFactory(); //這里是實現工廠類
ret = get.GetResult();
return ret;
}
//public string get()
//{
// return "get";
//}
//public string gettest()
//{
// return "gettest";
//}
//public string getrset()
//{
// return "getset";
//}
}

15:50修改補充如下:
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 中西区| 葵青区| 饶阳县| 旺苍县| 犍为县| 双辽市| 威远县| 本溪| 丹棱县| 奇台县| 梅州市| 房产| 隆昌县| 屏东县| 新津县| 开远市| 富顺县| 邢台市| 襄汾县| 东阿县| 五峰| 临漳县| 丰宁| 吐鲁番市| 台东市| 交口县| 印江| 黄浦区| 南宫市| 棋牌| 东城区| 贡嘎县| 澳门| 永顺县| 东乌| 边坝县| 蒙山县| 克山县| 凤翔县| 红桥区| 吉隆县|