格式化传递给Java中的函数的多个参数 [英] Formatting multiple arguments passed to a function in Java
本文介绍了格式化传递给Java中的函数的多个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$ $ p $ $ $ $ $ $ $ $ $ $ $ $ .getNumberOfParents(),
dataManager.getLastUpdate()。getNumberOfGrandChildren(),
long milliseconds,
int somethingelse)
在 Java
中提供了一种方法来对齐参数吗?拟合一行中的所有参数不会看起来漂亮。
解决方案
当表达式不会按照这些一般原则打破它:
- 以逗号分隔。
- 在操作符之前中断。
- 将较高级别的分隔符设置为较低级别的分隔符。
- 将新行与表达式的开头对齐
- 如果上面的规则导致代码混淆或代码被压缩到正确的空白处,那么只需要缩进8个空格即可。
该文档还包含一些方法调用的例子:
函数(longExpression1,longExpression2,longExpression3,
longExpress ion4,longExpression5);
var = function1(longExpression1,
function2(longExpression2,
longExpression3));
Often the number of arguments passed to a function can be large. Consider the following case:
calculate(dataManager.getLastUpdate().getNumberOfChildren(),
dataManager.getLastUpdate().getNumberOfParents(),
dataManager.getLastUpdate().getNumberOfGrandChildren(),
long milliseconds,
int somethingelse)
Is there a guideline in Java
that offers a way to align the arguments? Fitting all arguments in a line would not look pretty.
解决方案
According to the Sun's Java coding conventions, paragraph 4.1 "Wrapping Lines":
When an expression will not fit on a single line, break it according to these general principles:
- Break after a comma.
- Break before an operator.
- Prefer higher-level breaks to lower-level breaks.
- Align the new line with the beginning of the expression at the same level on the previous line.
- If the above rules lead to confusing code or to code that’s squished up against the right margin, just indent 8 spaces instead.
The document also includes some examples for method calls:
function(longExpression1, longExpression2, longExpression3,
longExpression4, longExpression5);
var = function1(longExpression1,
function2(longExpression2,
longExpression3));
这篇关于格式化传递给Java中的函数的多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文