迭代方法参数

发布于 2024-10-06 20:37:08 字数 486 浏览 0 评论 0原文

这是一个简单且可能微不足道的问题:在 Java 中,如何迭代传递给我正在工作的方法的参数?

我需要它来 trim() 所有字符串。

编辑

更准确地说使用示例可以是这样的(用伪代码编写,反映了我希望它如何工作):

public void methodName(String arg1, int arg2, int arg3, String arg4, double arg5)
    for(Object obj : getThisMethod().getParameters() )
        System.out.println(obj.getName() + " = " + obj.toString())

重点是 getThisMethod ().getParameters()。我必须在那个地方写什么?

It's a simple and maybe trivial question: in Java how can I iterate through the parameters passed to the method where I'm working?

I need it to trim() all of them that are strings.

EDIT

To be more precise an example of use can be this (written in a pseudo-code reflecting how I wish it to work):

public void methodName(String arg1, int arg2, int arg3, String arg4, double arg5)
    for(Object obj : getThisMethod().getParameters() )
        System.out.println(obj.getName() + " = " + obj.toString())

The point is that getThisMethod().getParameters(). What must I write in that place?

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

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

发布评论

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

评论(5

じ违心 2024-10-13 20:37:08

如果您的函数使用 Varargs 这非常简单:

private void yourFunction(String... parameters) {
  for (String parameter : parameters) {
    // Do something with parameter
  }
}

If your function uses Varargs this is pretty straightforward:

private void yourFunction(String... parameters) {
  for (String parameter : parameters) {
    // Do something with parameter
  }
}
等风也等你 2024-10-13 20:37:08

单个参数不可迭代;你需要为此收集一个集合。

如果它们是单独的绳子,你只需要拿一把铲子即可。

如果您有太多的字符串参数,这让人感到压抑,也许您的方法需要重新考虑。要么所有这些参数都应该封装到一个对象中,因为它们是相关的,要么该方法试图做太多事情。这对我来说缺乏凝聚力。

Individual parameters aren't iterable; you'd need a collection for that.

You'll just have to get a shovel if they're individual Strings.

If you have so many String parameters that this is oppressive, perhaps your method needs to be re-thought. Either all those parameters should be encapsulated into an object, because they're related, or that method is trying to do too much. It speaks to a lack of cohesion to me.

┈┾☆殇 2024-10-13 20:37:08

您尝试解决的任务只能使用 AOP(面向方面​​编程)框架来实现。

AOP 框架允许您向方法添加一些代码而无需更改它。实际上,他们创建了代理类来包装您的原始类,并在您绑定它们的每个方法之前执行所需的代码行。

然而,AOP 对于一些简单的任务来说有点大材小用,因为它通常需要一些复杂的配置,并且通常需要与 DI 框架集成。

如果您仍然感兴趣,这里有一些 AOP 框架列表: http://java-source .net/open-source/aspect-oriented-frameworks

编辑:

实际上,我认为您一开始就以错误的方式完成任务。如果您的方法是业务层的一部分 - 它不应该允许未修剪的参数并在这种情况下抛出某种异常。如果您的方法是某些表示层的一部分 - 它应该手动清理参数,通常在从用户读取参数的部分附近。

例如,如果您正在从某些 Swing 表单读取该参数,那么您应该在传递给构造函数之前修剪它们。例如:

您当前的代码:

int someId = Integer.valueOf(idField.getText());
String someName = nameField.getText();
String someArg = argField.getText();
new Constructor(someId, someName, someArg)

应该如何处理:

int someId = Integer.valueOf(idField.getText());
String someName = nameField.getText().trim();
String someArg = argField.getText().trim();
new Constructor(someId, someName, someArg)

The task you are trying solve is only possible using AOP (Aspect Oriented Programming) frameworks.

AOP frameworks allow you to add some code to the method without changing it. In reality they create Proxy classes that wrap around your original classes and execute required lines of code before each method you bind them too.

However, AOP is an overkill for some simple tasks as it usually requires some complex configurations and usually integration with DI frameworks.

Here's some list of AOP frameworks if you are still interested: http://java-source.net/open-source/aspect-oriented-frameworks.

Edit:

Actually, I think that you are doing your task the wrong way in first place. If your method is a part of Business Layer - it should not allow non-trimmed parameters and throw some kind of Exception in that case. If your method is part of some Presentation Layer - it should be cleaning the parameters manually, usually near the part where it reads the parameters from the user.

For example, if you are reading that parameters from some Swing form, then you should trim them before passing to your Constructor. For example:

Your current code:

int someId = Integer.valueOf(idField.getText());
String someName = nameField.getText();
String someArg = argField.getText();
new Constructor(someId, someName, someArg)

How it should be handled:

int someId = Integer.valueOf(idField.getText());
String someName = nameField.getText().trim();
String someArg = argField.getText().trim();
new Constructor(someId, someName, someArg)
一刻暧昧 2024-10-13 20:37:08

因为您可以将方法更改为以下方法,所以您可以迭代它们。

public void method(String... args)

For you can change your method to be the following you can iterate over them.

public void method(String... args)
毁梦 2024-10-13 20:37:08

如果您的问题是如何从对象中识别字符串,您可以这样做:

if (myObject instanceof String) {
  // My param is a string!
}

If your question is how to recognise a String from an Object, you can do that:

if (myObject instanceof String) {
  // My param is a string!
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文