首页 > Windows开发 > 详细

C# 最简单的链式调用例子

时间:2020-02-23 11:25:50      阅读:122      评论:0      收藏:0      [点我收藏+]

链式调用,重复使用一个初始化操作,来达到少量代码表达复杂操作的目的。

直接看例子

 1     public class Student
 2     {
 3         private string name;
 4         private int age;
 5 
 6         public Student() { }
 7 
 8         public Student setName(string name)
 9         {
10             this.name = name;
11             System.Console.WriteLine(name);
12             return this;
13         }
14 
15         public Student setAge(int age)
16         {
17             this.age = age;
18             System.Console.WriteLine(age);
19             return this;
20         }
21     }
1 Student person = new Student();
2 person.setAge(12).setName("Jack");//通过链式调用

技术分享图片

如果SetAge() 和 SetName()的返回类型不是类本身的话,比如返回string、int就不能用这种方式了。

C# 最简单的链式调用例子

原文:https://www.cnblogs.com/YourDirection/p/12348440.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!