在C#中使用Collection初始化器并允许在不必调用构造函数的情况下定义类的属性,在C#中使用Method Chaining有什么意义吗?我什么也看不见.也许我在这里遗漏了什么?
谢谢
假设我有一个很长的链接方法
object.SetA(123).SetB(234).SetC(345) ...
Run Code Online (Sandbox Code Playgroud)
什么是最好的缩进?所有编译器都支持它们?
object.
SetA(123).
SetB(234).
SetC(345) ...
Run Code Online (Sandbox Code Playgroud)
要么
object
.SetA(123)
.SetB(234)
.SetC(345) ...
Run Code Online (Sandbox Code Playgroud) 假设我有一个带有以下两个签名的重载扩展方法:
public static void MyExtensionMethod(this Foo foo);
public static void MyExtensionMethod(this Foo foo, Bar bar);
Run Code Online (Sandbox Code Playgroud)
我想将一种方法链接到另一种方法.我可以通过以下两种方式之一来做到这一点:
链接技术#1
public static void MyExtensionMethod(this Foo foo)
{
// Call overload using extension method syntax.
foo.MyExtensionMethod(new Bar());
}
public static void MyExtensionMethod(this Foo foo, Bar bar)
{
// Do stuff...
}
Run Code Online (Sandbox Code Playgroud)
链接技术#2
public static void MyExtensionMethod(this Foo foo)
{
// Call overload as a regular method.
MyExtensionMethod(foo, new Bar());
}
public static void MyExtensionMethod(this Foo foo, Bar bar)
{
// Do stuff...
}
Run Code Online (Sandbox Code Playgroud)
这是我的问题:调用重载方法作为扩展方法与常规方法之间有什么区别吗?如果是这样,有什么区别?一个比另一个好吗?
我想在Point下面的课上进行方法链接.
#include <iostream>
class Point {
public:
Point(int x, int y): _x(x), _y(y) {}
Point& moveX(int x);
Point& moveY(int y);
Point& print() const;
...
};
...
Point& Point::print() const {
std::cout << "(" << _x << "," << _y << ")" << std::endl;
return *this; // Compile fails
}
Run Code Online (Sandbox Code Playgroud)
我认为将其标记print()为const成员函数是有意义的,因为它只是打印内部成员.但是,我想在非const和const函数之间进行方法链接,如下所示.
int main() {
Point p(1,1);
p.moveX(10).print().moveY(11); // method chaining
}
Run Code Online (Sandbox Code Playgroud)
所以我必须返回this非const,但它不能编译,因为在我的理解中,成员被标记为const包括thisconst成员函数.
在这种情况下,有没有办法进行方法链接?
我想做以下事情:
object.method1.method2.method3.method4
Run Code Online (Sandbox Code Playgroud)
除了所有方法都在一个数组中.所以,在一个例子中:
object = 1
methods = %W(to_s split shift)
# should somehow do 1.to_s.split.shift
Run Code Online (Sandbox Code Playgroud)
如何methods在对象上调用顺序?
我似乎无法弄清楚如何将这些功能链接在一起,任何帮助或建议都将不胜感激。
// Generic approach to adding flags to a command string
trait UpdateCommandString {
def update[T](option: Option[T], flagName: String)(implicit command: String): String = {
if (option.isEmpty)
command
else if (option.get.isInstanceOf[Boolean]) {
if (option.get.asInstanceOf[Boolean])
s"$command $flagName"
command
} else
s"$command $flagName ${option.get.asInstanceOf[String]}"
}
}
// One example of flags (the program I'm using has literally 50+ flags
// so there will be a number of case classes that group them into related
// sets)
case class Flags(cache: Option[String] = None,
errorlog: …Run Code Online (Sandbox Code Playgroud) 在线阅读后,我写了这个简单的代码,通过链接进行加法和乘法.但是,阅读代码,对我来说,"结果"方法使代码的可读性降低,而且看起来有点多余.有人可以帮我摆脱结果功能吗?
var Calculator = function () {
var result = 0;
this.Add = function (x) {
result = result + x;
return this;
};
this.Multiply = function (x) {
result = result * x;
return this;
};
this.Result = function () {
return result;
}
};
var total = new Calculator().Add(2).Add(3).Multiply(5);
alert(total.Result());
Run Code Online (Sandbox Code Playgroud)
我想要实现的是
var total = new Calculator().Add(2).Add(3).Multiply(5);
alert(total);
Run Code Online (Sandbox Code Playgroud) 通过 Java 8 中引入的函数式接口,您可以轻松地将不同的表达式链接到一个新表达式中,如下面的代码片段所示。
public class PredicateChaining {
public static void main(String[] args) {
// verbose, but standard JDK functionality only
Predicate<String> allUpperCase = StringUtils::isAllUpperCase;
Predicate<String> longerThan5 = s -> s.length() > 5;
if (allUpperCase.and(longerThan5).test("PREDICATE")) {
System.out.println("'PREDICATE' is a uppercase text, longer than 5.");
}
// compact, but with utility method ('chain' in this case)
if (chain(StringUtils::isAllLowerCase).and(s -> s.length() < 5).test("test")) {
System.out.println("'test' is a lowercase text, shorter than 5.");
}
}
public static <T> Predicate<T> chain(Predicate<T> test) {
return …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过继承支持构建器模式,以允许父级和子级的设置链没有问题。为此,我需要父类知道返回子类型,以使所有方法公开以进行链接。
这是我在本文之后编写的代码示例,我认为它应该可以工作。但是,正如您将看到的,使用a1的第一个示例可以正常工作,但是如果我更改设置器的顺序,则无法识别最后一个设置器。
问题是:一旦我从父类中调用了一个方法,它将以类型而不是子类型返回自身,即使认为T被定义为子类型。
public class JavaApplication1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
A a1 = new B<>().setAge(0).setId(0).setName("name"); //Works fine
A a2 = new B<>().setAge(0).setName("name").setId(0); //setId is not found
}
static class A<T extends A> {
private String mpName;
T setName(String name) {
mpName = name;
return (T) this;
}
}
static class B<T extends B> extends A<T> {
private int mpAge;
private …Run Code Online (Sandbox Code Playgroud) 在测试套件上,我想用链接方法模拟模型findOne然后select
public loggingIn = async (loginDTO: LoginDTO) => {
const user = await UserModel.findOne({ email : loginDTO.email }).select(['_id', 'username', 'email', 'password']);
if (user) {
const isPasswordMatching = await bcrypt.compare(loginDTO.password, user.password);
if (isPasswordMatching) {
const token = this.generateToken(user);
const tokenDTO : TokenDTO = {
access_token: token,
expiresIn: loginConstant.EXPIRES_IN,
};
return tokenDTO;
}
throw new InvalidCrendentialsException();
}
throw new InvalidCrendentialsException();
}
Run Code Online (Sandbox Code Playgroud)
it('should return access_token when login is success', async () => {
UserModel.findOne = jest.fn().mockResolvedValueOnce(UserFactory.successResponse);
bcrypt.compare = …Run Code Online (Sandbox Code Playgroud) method-chaining ×10
c# ×2
c++ ×2
java ×2
generics ×1
indentation ×1
java-8 ×1
javascript ×1
jestjs ×1
lambda ×1
mocking ×1
node.js ×1
ruby ×1
scala ×1
unit-testing ×1