欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁 > 軟件編程 > C#教程 >

C#實(shí)現(xiàn)類似jQuery的方法連綴功能

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊: 次

jQuery的方法連綴使用起來非常方便,可以簡化語句,讓代碼變得清晰簡潔。那C#的類方法能不能也實(shí)現(xiàn)類似的功能呢?基于這樣的疑惑,研究了一下jQuery的源代碼,發(fā)現(xiàn)就是需要方法連綴的函數(shù)方法最后返回對象本身即可。既然javascript可以,C#應(yīng)該也是可以的。
為了驗(yàn)證,編寫一個(gè)jQPerson類,然后用方法連綴對其ID,Name,Age等屬性進(jìn)行設(shè)置,請看下面的代碼:

 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>
   /// 設(shè)置ID,返回this,即jQPerson實(shí)例
   /// </summary>
   /// <param name="Id"></param>
   /// <returns></returns>
   public jQPerson setId(string Id)
   {
    this.Id = Id;
    return this;
   }
   /// <summary>
   /// 返回this,即jQPerson實(shí)例
   /// </summary>
   /// <param name="name"></param>
   /// <returns></returns>
   public jQPerson setName(string name)
   {
 
    this.Name = name;
    return this;
   }
   /// <summary>
   /// 返回this,即jQPerson實(shí)例
   /// </summary>
   /// <param name="age"></param>
   /// <returns></returns>
   public jQPerson setAge(int age)
   {
 
    this.Age = age;
    return this;
   }
   /// <summary>
   /// 返回this,即jQPerson實(shí)例
   /// </summary>
   /// <param name="sex"></param>
   /// <returns></returns>
   public jQPerson setSex(string sex)
   {
 
    this.Sex = sex;
    return this;
   }
   /// <summary>
   /// 返回this,即jQPerson實(shí)例
   /// </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);
 
 
   }
 
  }
 } 

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

/// <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("驗(yàn)證此測試方法的正確性。");
   }

通過以上操作可以看出,方法連綴功能的確使代碼變得直觀和簡潔,增加可閱讀性,大家不妨試一試。

上一篇:C#編程實(shí)現(xiàn)對象與JSON串互相轉(zhuǎn)換實(shí)例分析

欄    目:C#教程

下一篇:C#影院售票系統(tǒng)畢業(yè)設(shè)計(jì)(4)

本文標(biāo)題:C#實(shí)現(xiàn)類似jQuery的方法連綴功能

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6858.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有