C# Fluent Interface是如何实现的呢?首先我们来看看Fluent Interface,直译过来是 “流畅(流利)的接口”,照字面有点难以理解,那么Fluent Interface是如何实现的呢?下面让我们从代码上向你介绍:

站在用户的角度思考问题,与客户深入沟通,找到文成网站设计与文成网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都网站设计、网站制作、外贸营销网站建设、企业官网、英文网站、手机端网站、网站推广、空间域名、虚拟空间、企业邮箱。业务覆盖文成地区。
C# Fluent Interface代码实现:
- public interface IRect
 - {
 - void SetWidth(int width);
 - void SetHeight(int height);
 - }
 - public Rect : IRect
 - {
 - private int _width;
 - private int _height;
 - public void SetWidth(int width) { this._width = width; }
 - public void SetHeight(int height){ this_height = height; }
 - }
 - public static void Main(string [] args)
 - {
 - IRect rect = new Rect();
 - rect.SetHeight(10);
 - rect.SetWidth(50);
 - }
 
没有什么花俏的东西,一个可设长宽的矩形接口并提供一个简单实现。接下来看看用另一种方式
- public interface IRectFluent
 - {
 - IRectFluent SetWidth(int width);
 - IRectFluent SetHeight(int height);
 - }
 - public RectFluent : IRectFluent
 - {
 - private int _width;
 - private int _height;
 - public IRectFluent SetWidth(int width) { this._width = width; return this; }
 - public IRectFluent SetHeight(int height){ this_height = height; return this; }
 - }
 - public static void Main(string [] args)
 - {
 - IRectFluent rect = new RectFluent();
 - rect.SetHeight(10).SetWidth(50); // checkpoint
 - }
 
这种“链式"方法调用方式是不是更接近我们人脑的思维方式,更简洁呢。没错, It's Fluent Interface。
个人理解的Fluent Interface 就是 在面向对象编程中,使用某种方式(通常但不限于使用 方法链方式)来实现更具可读性,易用性的编程方式。而方法链的关键之处就是在方法内部调用***要返回调用者本身。
所谓Fluent借助于wikipedia的说法就是‘This style is beneficial due to its ability to provide a more fluid feel to the code."
说到这里,经常使用jquery的朋友肯定感觉很熟悉上面的使用方式。
没错,类似于 $('id').show().css('').fadeOut(); 这种就是一种Fluent Interface实现。
C# Fluent Interface的相关内容就向你介绍到这里,希望对你了解和学习C# Fluent Interface有所帮助。
【编辑推荐】
                当前题目:C# Fluent Interface概念实例浅析
                
                分享地址:http://www.csdahua.cn/qtweb/news42/31092.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网