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

首頁 > 學院 > 開發設計 > 正文

C#實現jQuery的方法連綴

2019-11-14 13:47:09
字體:
來源:轉載
供稿:網友

   jQuery的方法連綴使用起來非常方便,可以簡化語句,讓代碼變得清晰簡潔。那C#的類方法能不能也實現類似的功能呢?基于這樣的疑惑,研究了一下jQuery的源代碼,發現就是需要方法連綴的函數方法最后返回對象本身即可。既然javascript可以,C#應該也是可以的。

  為了驗證,編寫一個jQPerson類,然后用方法連綴對其ID,Name,Age等屬性進行設置,請看下面的代碼:

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6  7 namespace CSharpMethodLikeJQuery 8 { 9     public class jQPerson10     {11         string Id { set; get; }12         string Name { set; get; }13         int Age { set; get; }14         string Sex { set; get; }15         string Info { set; get; }16 17         public jQPerson()18         {19 20         }21         /// <summary>22         /// 設置ID,返回this,即jQPerson實例23         /// </summary>24         /// <param name="Id"></param>25         /// <returns></returns>26         public jQPerson setId(string Id)27         {28             this.Id = Id;29             return this;30         }31         /// <summary>32         /// 返回this,即jQPerson實例33         /// </summary>34         /// <param name="name"></param>35         /// <returns></returns>36         public jQPerson setName(string name)37         {38 39             this.Name = name;40             return this;41         }42         /// <summary>43         /// 返回this,即jQPerson實例44         /// </summary>45         /// <param name="age"></param>46         /// <returns></returns>47         public jQPerson setAge(int age)48         {49 50             this.Age = age;51             return this;52         }53         /// <summary>54         /// 返回this,即jQPerson實例55         /// </summary>56         /// <param name="sex"></param>57         /// <returns></returns>58         public jQPerson setSex(string sex)59         {60 61             this.Sex = sex;62             return this;63         }64         /// <summary>65         /// 返回this,即jQPerson實例66         /// </summary>67         /// <param name="info"></param>68         /// <returns></returns>69         public jQPerson setInfo(string info)70         {71 72             this.Info = info;73             return this;74         }75         /// <summary>76         /// tostring輸出鍵值對信息77         /// </summary>78         /// <returns></returns>79         public string toString()80         {81 82             return string.Format("Id:{0},Name:{1},Age:{2},Sex:{3},Info:{4}", this.Id, this.Name, this.Age, this.Sex, this.Info);83 84 85         }86 87     }88 }

  然后可以對上面進行測試,看方法連綴是否生效:

 1         /// <summary> 2         ///toString 的測試 3         ///</summary> 4         [TestMethod()] 5         public void toStringTest() 6         { 7             jQPerson target = new jQPerson(); 8             target.setId("2") 9                   .setName("jack")10                   .setAge(26)11                   .setSex("man")12                   .setInfo("ok");13             string expected = "Id:2,Name:jack,Age:26,Sex:man,Info:ok";14             string actual;15             actual = target.toString();16             Assert.AreEqual(expected, actual);17             //Assert.Inconclusive("驗證此測試方法的正確性。");18         }

  可以看到,方法連綴確實可以讓代碼變得直觀和簡潔,增加可閱讀性。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 原平市| 横山县| 三河市| 邵阳县| 吴旗县| 晴隆县| 岗巴县| 沐川县| 鸡西市| 中山市| 托克逊县| 马山县| 长泰县| 深圳市| 曲松县| 淅川县| 荃湾区| 紫阳县| 乐安县| 泗洪县| 浮山县| 巧家县| 东台市| 手游| 绥江县| 阿鲁科尔沁旗| 闽清县| 弥渡县| 日土县| 万宁市| 甘洛县| 成安县| 苍梧县| 白山市| 洮南市| 武夷山市| 曲麻莱县| 临邑县| 玛多县| 德江县| 陆丰市|