zoukankan      html  css  js  c++  java
  • C#实现jQuery的方法连缀

     

    jQuery的方法连缀使用起来非常方便,可以简化语句,让代码变得清晰简洁。那C#的类方法能不能也实现类似的功能呢?基于这样的疑惑,研究了一下jQuery的源代码,发现就是需要方法连缀的函数方法最后返回对象本身即可。既然javascript可以,C#应该也是可以的。

      为了验证,编写一个jQPerson类,然后用方法连缀对其ID,Name,Age等属性进行设置,请看下面的代码:

    using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      
      namespace CSharpMethodLikeJQuery
      {
          public class jQPerson
         {
             string Id { set; get; }
             string Name { set; get; }
             int Age { set; get; }
             string Sex { set; get; }
             string Info { set; get; }
     
             public jQPerson()
             {
     
             }
             /// <summary>
             /// 设置ID,返回this,即jQPerson实例
             /// </summary>
             /// <param name="Id"></param>
             /// <returns></returns>
             public jQPerson setId(string Id)
             {
                 this.Id = Id;
                 return this;
             }
             /// <summary>
             /// 返回this,即jQPerson实例
             /// </summary>
             /// <param name="name"></param>
             /// <returns></returns>
             public jQPerson setName(string name)
             {
     
                 this.Name = name;
                 return this;
             }
             /// <summary>
             /// 返回this,即jQPerson实例
             /// </summary>
             /// <param name="age"></param>
             /// <returns></returns>
             public jQPerson setAge(int age)
             {
     
                 this.Age = age;
                 return this;
             }
             /// <summary>
             /// 返回this,即jQPerson实例
            /// </summary>
            /// <param name="sex"></param>
             /// <returns></returns>
             public jQPerson setSex(string sex)
             {
     
                 this.Sex = sex;
                 return this;
           }
             /// <summary>
             /// 返回this,即jQPerson实例
             /// </summary>
             /// <param name="info"></param>
             /// <returns></returns>
             public jQPerson setInfo(string info)
             {
     
                this.Info = info;
                 return this;
            }
             /// <summary>
             /// tostring输出键值对信息
            /// </summary>
             /// <returns></returns>
             public string toString()
           {
    
                 return string.Format("Id:{0},Name:{1},Age:{2},Sex:{3},Info:{4}", this.Id, this.Name, this.Age, this.Sex, this.Info);
     
    
            }
     
         }
     }

    然后可以对上面进行测试,看方法连缀是否生效:

    /// <summary>
              ///toString 的测试
              ///</summary>
              [TestMethod()]
              public void toStringTest()
              {
                  jQPerson target = new jQPerson();
                  target.setId("2")
                        .setName("jack")
                       .setAge(26)
                       .setSex("man")
                       .setInfo("ok");
                 string expected = "Id:2,Name:jack,Age:26,Sex:man,Info:ok";
                 string actual;
                 actual = target.toString();
                 Assert.AreEqual(expected, actual);
                 //Assert.Inconclusive("验证此测试方法的正确性。");
             }

    可以看到,方法连缀确实可以让代码变得直观和简洁,增加可阅读性。

  • 相关阅读:
    docker从容器中怎么访问宿主机
    fail2ban CentOS7安装
    关于sql当中的isnull和ifnull的区别
    关于mybatis反向生成的时候,对于数据库当中的数据类型是text的处理。
    关于Java当中的MapUtils工具类的使用和注意事项。
    在大数据管理平台HDP的ambria的时候遇到python的安全认证的问题
    通过java代码实现调用excel当中的宏的操作。
    通过vba实现对word当中个的标签进行批量的替换
    通过vba实现对word当中的表格动态的插入行,同时对表中进行数据的填充。
    关于vba实现删除word文档中的指定表格。
  • 原文地址:https://www.cnblogs.com/ldyblogs/p/likejQuery.html
Copyright © 2011-2022 走看看