是否有与 Javascript 的 with 语句等效的 Java?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13854448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a Java equivalent to Javascript's with statement?
提问by Ian Campbell
Is there a similar way to declare a with-statementin Java (as in Javascript), or are there structural reasons why this would not be possible?
在 Java 中
是否有类似的方法来声明with 语句(如在 Javascript 中),或者是否存在结构性原因为什么这不可能?
例如,这个 Javascript:
with(obj)
{
getHomeworkAverage();
getTestAverage();
getAttendance();
}
...is nice and easy. However, it would seem that method calls haveto be chained to their object(s) everytime in Java, with no such graceful shortcuts avaiable:
...很好很容易。然而,似乎每次在 Java中方法调用都必须链接到它们的对象,没有这样优雅的快捷方式可用:
obj.getHomeworkAverage();
obj.getTestAverage();
obj.getAttendance();
This is very redundant, and especially irritating when there are many methods to call.
这是非常多余的,尤其是在有很多方法要调用的时候很烦人。
- So, is there any similar way to declare a with-statementin Java?
- And if this is notpossible, what are the reasons that it ispossible in Javascript as compared to notpossible in Java?
- 那么,有没有类似的方法在 Java 中声明一个with 语句?
- 如果这是没有可能的,原因是什么,这是可能的Javascript相比,没有在Java中可能吗?
回答by Patricia Shanahan
There is no direct equivalent of "with".
没有直接等价的“with”。
If the methods are instance methods, you can give the target object reference a short identifier for use in a block:
如果方法是实例方法,您可以为目标对象引用一个短标识符以在块中使用:
{
Student s = student;
s.getHomeworkAverage();
s.getTestAverage();
s.getAttendance();
}
If the methods are static, you can use "import static":
如果方法是静态的,您可以使用“import static”:
import static java.lang.Math.*;
public class Test {
public static void main(String[] args) {
System.out.println(sqrt(2));
}
}
回答by Codo
No, there's no withstatement or a similar construct in Java.
不,Java 中没有with语句或类似的构造。
回答by Karthik T
If the class of obj
is under your control, you could provide a Fluent interface, basically returning this
in every function. This would let you chain method calls like this-
如果类obj
在你的控制之下,你可以提供一个Fluent interface,基本上this
在每个函数中返回。这会让你像这样链接方法调用 -
obj.getHomeworkAverage().getTestAverage().getAttendance();
obj.getHomeworkAverage().getTestAverage().getAttendance();
回答by Snakes and Coffee
There is a reason why you can't do this in Java. Perhaps the most obvious is that functions are not first-class objects in Java and therefore you cannot simply have a name referring to the function - it must be under a class. As mentioned by Karthik T, the way you would shorten this could be just creative use of whitespace:
您不能在 Java 中执行此操作是有原因的。也许最明显的是,函数不是 Java 中的一流对象,因此您不能简单地使用一个名称来指代该函数——它必须在一个类下。正如 Karthik T 所提到的,缩短它的方法可能只是创造性地使用空格:
obj
.meth1()
.meth2()
.meth3()
where each method returns the object.
其中每个方法返回对象。
For more info on First-Class Functions: wikipedia
有关一流函数的更多信息:维基百科
回答by Krease
So, is there any similar way to declare a with-statement in Java?
那么,有没有类似的方法在 Java 中声明一个 with 语句?
No, there is not. The closest thing would be the import static
mechanism described in Patricia Shanahan's answer.
不,那里没有。最接近的是import static
Patricia Shanahan 的回答中描述的机制。
And if this is not possible, what are the reasons that it is possible in Javascript as compared to not possible in Java?
如果这是不可能的,与在 Java 中不可能相比,在 Javascript 中可能的原因是什么?
They're two entirely different languages with different features/strengths/weaknesses. An analogy: A hammer and a screwdriver are both tools, but they are used in different ways.
它们是两种完全不同的语言,具有不同的特性/优势/劣势。打个比方:锤子和螺丝刀都是工具,但使用方式不同。