C#实现jQuery的方法连缀
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 jQPerson
10 {
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 }
可以看到,方法连缀确实可以让代码变得直观和简洁,增加可阅读性。
水平有限,望各位园友不吝赐教!如果觉得不错,请点击推荐和关注!
出处:http://www.cnblogs.com/isaboy/
声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://www.cnblogs.com/isaboy/
声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?