格式化传递给Java中的函数的多个参数 [英] Formatting multiple arguments passed to a function in Java

查看:535
本文介绍了格式化传递给Java中的函数的多个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常传递给一个函数的参数个数可能很大。考虑以下情况:

$ $ p $ $ $ $ $ $ $ $ $ $ $ $ .getNumberOfParents(),
dataManager.getLastUpdate()。getNumberOfGrandChildren(),
long milliseconds,
int somethingelse)

Java 中提供了一种方法来对齐参数吗?拟合一行中的所有参数不会看起来漂亮。

解决方案

根据
lockquote

当表达式不会按照这些一般原则打破它:


该文档还包含一些方法调用的例子:

 函数(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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆