C# 如何调用函数序列?
实际上我需要知道这行代码是如何执行的 例如:C# 如何调用函数序列?,c#,c++,visual-studio-2010,generics,C#,C++,Visual Studio 2010,Generics,实际上我需要知道这行代码是如何执行的 例如: Browser("InternetExplorer").Page("Stackoverflow").WebElement("textbox").set "user" 上述行的执行方式类似于将浏览器设置为Internet Explorer,在其中查找页面“stackoverflow”,然后在其中查找webelement“textbox”,然后将其值设置为“user”。这样就完成了操作 我想知道这个序列调用是如何完成的。我不想知道如何将浏览器设置为In
Browser("InternetExplorer").Page("Stackoverflow").WebElement("textbox").set "user"
上述行的执行方式类似于将浏览器设置为Internet Explorer,在其中查找页面“stackoverflow”,然后在其中查找webelement“textbox”,然后将其值设置为“user”。这样就完成了操作
我想知道这个序列调用是如何完成的。我不想知道如何将浏览器设置为Internet Explorer等
我需要执行一个简单的语句,比如
乐趣(“添加”)。值(“2,3”)。计算
我需要通过调用“add”函数来执行上面的行,然后值“2,3”作为参数传递,然后“compute”将其添加,最终结果应该是“5”
如何做到这一点?我们是否必须使用不同的类来获得“乐趣”和“价值”,或者我们需要将它们实现为同一类的“函数”
如何处理序列调用?返回对现有对象的引用即可实现此效果:
class Operator
{
public:
Operator(const string& opAsStr)
{
...
}
Operator& Values(const string& operands)
{
....
return *this;
}
int Compute() // Compute must be a function, no properties in C++
{
...
}
};
// Usable like this
Operator("Add").Values("2,3").Compute()
通过定义更多返回函数*this
,可以链接许多调用。请注意,您可以返回一个值(即运算符
,而不是引用,或者常量引用,具体取决于您的用例)。
还可以返回对另一个类的对象的引用(或值):
class A
{
public:
void DoSomething()
{
....
}
};
class B
{
public:
A MakeA()
{
return A();
}
};
B().MakeA().DoSomething();
返回对现有对象的引用即可实现此效果:
class Operator
{
public:
Operator(const string& opAsStr)
{
...
}
Operator& Values(const string& operands)
{
....
return *this;
}
int Compute() // Compute must be a function, no properties in C++
{
...
}
};
// Usable like this
Operator("Add").Values("2,3").Compute()
通过定义更多返回函数*this
,可以链接许多调用。请注意,您可以返回一个值(即运算符
,而不是引用,或者常量引用,具体取决于您的用例)。
还可以返回对另一个类的对象的引用(或值):
class A
{
public:
void DoSomething()
{
....
}
};
class B
{
public:
A MakeA()
{
return A();
}
};
B().MakeA().DoSomething();