Java什么都不做
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22493765/
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
Java do nothing
提问by goodcow
What is the equivalent to Python's pass
in Java? I realize that I could use continue
or not complete the body of a statement to achieve that effect, but I like having a pass
statement.
pass
Java 中的Python 相当于什么?我意识到我可以使用continue
或不完成语句的主体来达到这种效果,但我喜欢有一个pass
语句。
采纳答案by James
Just use a semi-colon ;
, it has the same effect.
只需使用分号;
,它具有相同的效果。
回答by user2357112 supports Monica
;
;
is the empty statement. Usually, you don't need it - you can just put nothing in the brackets for an empty loop - but it can be useful.
;
是空语句。通常,你不需要它——你可以在空循环的括号中放任何东西——但它可能很有用。
回答by sabbahillel
If you want something noticeable, you can use
如果你想要一些引人注目的东西,你可以使用
assert true;
This will allow you to have something that a reader can recognize or that can be searched for.
这将使您拥有读者可以识别或可以搜索的内容。
回答by Mindaugas Bernatavi?ius
There is no (strict) equivalent since in java you have to specify the return type for the method in its declaration which is then checked against a computed type of the following the return statement. So for methods that have a return type - neither semicollon nor leaving empty braces will work;
没有(严格的)等价物,因为在 java 中,您必须在其声明中指定方法的返回类型,然后根据 return 语句后面的计算类型进行检查。因此,对于具有返回类型的方法 - 分号和空大括号都不起作用;
I personally use: throw new java.lang.UnsupportedOperationException("Not supported yet.");
- this is searchable, alerts you that the method is not implemented and is compatible with the strict return types.
我个人使用:throw new java.lang.UnsupportedOperationException("Not supported yet.");
- 这是可搜索的,提醒您该方法未实现并且与严格返回类型兼容。
回答by Gro
I feel, there is no construct in Java identical to pass in Python. This is mostly because Java is a statically typed language where as Python is a dynamically typed language. More so when you are defining a method / function. In that context, the provided answers are valid / correct only for a method that returns void.
我觉得,Java 中没有与 Python 中传递相同的构造。这主要是因为 Java 是一种静态类型语言,而 Python 是一种动态类型语言。当您定义方法/函数时更是如此。在这种情况下,提供的答案仅对返回 void 的方法有效/正确。
For example for a Python function
例如对于 Python 函数
def function_returns_void:
pass
you can have a Java method
你可以有一个 Java 方法
public void function_returns_void(){}
or
或者
public void function_returns_void(){;}
but when a method is supposed to return a value, while passmay still work in Python, one will stuck with compilation problem when not returning a value.
但是当一个方法应该返回一个值时,虽然pass在 Python 中可能仍然有效,但当不返回值时,就会陷入编译问题。
回答by Adam Burley
I normally use something like:
我通常使用类似的东西:
"".isEmpty(); // do nothing
It's useful to be able to have a line of code which does nothing when debugging. You can put a breakpoint on that line, even if it would usually be an empty block of code (e.g. empty catch block etc) which can create confusion about where the breakpoint is actually set.
能够有一行代码在调试时什么都不做是很有用的。您可以在该行上放置一个断点,即使它通常是一个空的代码块(例如空的 catch 块等),这可能会对实际设置断点的位置造成混淆。