我想知道Java是否有这样的形式:
new Object().methodOne("This is the first method").methodTwo("Second attached method");
new String("Hello World ").TrimEnd().Split(' ');
谢谢你
我想知道Java是否有这样的形式:
new Object().methodOne("This is the first method").methodTwo("Second attached method");
new String("Hello World ").TrimEnd().Split(' ');
谢谢你
您可以在 Java 中执行此操作。这取决于方法的返回类型。
特定的 API 可能不支持这一点,因为方法可能不会返回像这样容易使用的类型。但是 Java 最肯定支持访问对象的实例成员而不将它们分配给变量。
我想你可能追求的是流畅接口的概念(当然可以用 Java 来表达)。
是的,你可以在 Java 中做这种事情。例如:
class Test {
public Test method(int x) {
return this;
}
public Test method2(String y) {
return this;
}
}
那么你就可以:
new Test().method(5).method2("test");
这种可以将方法调用串在一起的接口称为流式接口。Martin Fowler(创造了这个术语)实际上首先使用 Java 演示了它。