方法链接不带括号,怎么办? [英] Method chaining without parentheses, how to?

查看:119
本文介绍了方法链接不带括号,怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个方法链,例如:

I would like to do a method chain, for example like this:

Car myCar = new Car();

// Chaining
myCar.config.engine.cylinders("4");

但是如何在不使用"config"或"engine"中的括号的情况下进行链接?

But how do I do the chaining, without using parentheses in "config" or "engine"?

我只能想像这样做:

myCar.config().engine().cylinders("4");

推荐答案

您可以通过在Car类中声明Config属性来做到这一点.然后是CarConfig类中的Engine属性,如下所示:

You can do this by declaring Config property in your Car class. Then Engine property in CarConfig class, like this:

public class Car
{
    public CarConfig Config { get; set; }
}

然后您就可以链接通话了.

Then you can chain the calls.

这篇关于方法链接不带括号,怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆