java 迭代方法参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4408059/
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
Iterating through method parameters
提问by bluish
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?
这是一个简单但也许微不足道的问题:在 Java 中,我如何遍历传递给我正在工作的方法的参数?
I need it to trim()
all of them that are strings.
我需要它给trim()
所有字符串。
EDIT
编辑
To be more precisean 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?
重点是getThisMethod().getParameters()
。我必须在那个地方写什么?
采纳答案by duffymo
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.
如果您有如此多的 String 参数,这令人沮丧,那么您的方法可能需要重新考虑。要么所有这些参数都应该封装到一个对象中,因为它们是相关的,要么该方法试图做太多事情。这说明我缺乏凝聚力。
回答by David
回答by bezmax
The task you are trying solve is only possible using AOP (Aspect Oriented Programming) frameworks.
您尝试解决的任务只能使用 AOP(面向方面的编程)框架。
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.
AOP 框架允许您在不更改方法的情况下向方法添加一些代码。实际上,它们创建了环绕原始类的代理类,并在您绑定它们的每个方法之前执行所需的代码行。
However, AOP is an overkill for some simple tasks as it usually requires some complex configurations and usually integration with DI frameworks.
然而,AOP 对于一些简单的任务来说是一种矫枉过正,因为它通常需要一些复杂的配置,并且通常需要与 DI 框架集成。
Here's some list of AOP frameworks if you are still interested: http://java-source.net/open-source/aspect-oriented-frameworks.
如果您仍然感兴趣,这里有一些 AOP 框架列表: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:
例如,如果您正在从某个 Swing 表单中读取该参数,那么您应该在传递给您的构造函数之前对其进行修剪。例如:
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)
回答by Peter Lawrey
For you can change your method to be the following you can iterate over them.
因为您可以将您的方法更改为以下您可以迭代它们。
public void method(String... args)
回答by Vincent Mimoun-Prat
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!
}