Java 关联数组作为方法的选项

发布于 2024-10-21 12:13:14 字数 762 浏览 5 评论 0原文

在与 PHP 交往了几年后,我正在重新学习 Java,我对 PHP 中使用的约定有一个疑问,以及如何在 Java 中实现类似的目标...

PHP

function getReport( $options = array() ) {

}

您将在上面的函数中传递 在一个可选的关联选项数组中,可能看起来像这样

Array(
    from => "20110325",
    to   => "20110413",
    subject_id => "2432",
    ...etc...
);

,如果没有传递参数,它会在没有选项的情况下正常处理。

下面是我在 java 中形成相同函数的尝试,因为它是强类型的(在某些情况下相当令人耳目一新),所以我在构建相同的灵活性时遇到了麻烦。我考虑过使用 Dictionary 但不确定这是否是“最佳实践”。

public TimeReport getTimeReport(Date from, Date to, int subjectId, int toDoItemId, int filterProjectId, int filterCompanyId) {

}

要使用一个/两个/无选项来调用它,它会变得非常丑陋,参数是......

getTimeReport(null,null,null,234,null,null);

I am re-learning Java after having an affair with PHP for some years, and I have a question about a convention used in PHP, and how I would accomplish a similar goal in Java...

PHP

function getReport( $options = array() ) {

}

in the above function you would be passing in an optional associative array of options that may look like this

Array(
    from => "20110325",
    to   => "20110413",
    subject_id => "2432",
    ...etc...
);

And if there is no argument passed, it processes fine with no options.

Below is my attempt to form the same function in java, being as it is Strongly typed (which is quite refreshing in some instances) I am having trouble building in the same flexibility. I've considered using a Dictionary but not sure how "best practice" that would be.

public TimeReport getTimeReport(Date from, Date to, int subjectId, int toDoItemId, int filterProjectId, int filterCompanyId) {

}

To call this with one/two/none options it gets pretty ugly with the arguments being...

getTimeReport(null,null,null,234,null,null);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑饮青盏花 2024-10-28 12:13:14

您可以使用另一个对象来调用 getTimeReport 来表示调用 getTimeReport 函数所需的参数吗?

当您可能想要传递对象时,看起来您习惯于将字符串传递给函数。

Can you call the getTimeReport with another object used to represent the parameters needed to invoke the getTimeReport function?

Looks like your used to passing strings to functions when you may want to be passing an object.

郁金香雨 2024-10-28 12:13:14

这种灵活性不是 Java 内置的,而且在我看来,该语言不太适合它。您可以检查每个输入参数;如果为 null,则将其设置为默认值。

为了更一般/说教,看起来您正在尝试拥有某种工厂(方法 getTimeReport() 属于哪个类?)。您想让 TimeReport 的构造函数处理这些选项吗?或者针对不同的情况制作不同的 TimeReport 构造函数?

This kind of flexibility is not built-in to Java, and the language is not very well suited to it in my opinion. You could check each input argument; if null, set it to a default value.

To be more general/didactic, it looks like you are trying to have a Factory of some sort (what class does the method getTimeReport() belong to?). Do you instead want to have the constructor of TimeReport handle these options? Or make different TimeReport constructors for different circumstances?

梦开始←不甜 2024-10-28 12:13:14

通常,您会引入一个表示报告生成的配置参数的类,例如 TimeReportSpecification 等。

然后,您可以使用静态工厂方法来为您提供 TimeReportSpecification 的实例,例如 withToDoItem,这样您就可以得到:

getTimeReport(TimeReportSpecification.withToDoItem(234));

Usually you would introduce a class representing the configuration parameters for the report generation, like TimeReportSpecification or something.

You can then have static factory methods that give you instances of a TimeReportSpecification, like withToDoItem, so you get:

getTimeReport(TimeReportSpecification.withToDoItem(234));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文