简体   繁体   English

关于Methods和ArrayList的清晰度

[英]Clarity about Methods and ArrayList

I would like to learn how can I create a ArrayList of methods. 我想学习如何创建方法的ArrayList。

public class a {
public static b (){}
public static c (){}
public static d (){}
public static e (){}

public static void main(String[] arg){
ArrayList<Method> lst = new ArrayList<Method>();
lst.add(1, a());
lst.add(2, b());
lst.add(3, c());
lst.add(4, d());
lst.add(5, e());
}

I'm wondering, why have it be like it, could someone be so kind to explain: 我想知道,为什么会这样,有人可以如此善意地解释:

public static Collection<? extends Method> a(){}
...
lst.addAll(1, a());

Why using Collection? 为何使用Collection? Why not use simply add(Object)? 为什么不简单地使用add(Object)?

Oh, one more thing. 哦,还有一件事。 How can I execute later methods from ArrayList? 如何从ArrayList执行以后的方法?

for (Method i : lst){
i;}

Thank you for your kind help. 感谢您的热心帮助。

If you want to use reflection, you should use getDeclaredMethod to get the method, as many other answers here have stated. 如果你想使用反射,你应该使用getDeclaredMethod来获取方法,正如这里的许多其他答案所述。 However, more common (and perhaps cleaner) practice in Java code, to avoid use of reflection, is to make an interface that you can provide implementations of: 但是,为避免使用反射,Java代码中更常见(也许更清晰)的实践是创建一个可以提供以下实现的接口:

private static interface Foo {
  public void doFoo();
}

public static Foo a = new Foo() {
  public void doFoo() {
    // Do 'a' stuff here.
  }
}

public static Foo b = new Foo() {
  public void doFoo() {
    // Do 'b' stuff here.
  }
}

...

public static Foo e = new Foo() {
  public void doFoo() {
    // Do 'e' stuff here.
  }
}

public static void main(String[] arg){
  ArrayList<Foo> lst = new ArrayList<Foo>();
  lst.add(1, a);
  lst.add(2, b);
  lst.add(3, c);
  lst.add(4, d);
  lst.add(5, e);
}

Then to invoke the function, you can call doFoo() on each of the Foo objects in the list. 然后,要调用该函数,可以在列表中的每个Foo对象上调用doFoo()

As of Java 7, you cannot treat a method as a variable as you're doing here. 从Java 7开始,您不能像在此处那样将方法视为变量。 You need to use Reflection to access the Method object that represents the method. 您需要使用Reflection来访问表示该方法的Method对象。 Use the getMethod method on the a class to retrieve the proper Method object, which you can add to your ArrayList . 使用getMethod方法a类来检索正确的Method的对象,您可以add到您ArrayList

The addAll method adds all elements in the collection argument to the collection on which you're calling addAll . addAll方法将collection参数中的所有元素添加到您调用addAll If you were to call add(Collection) , then the Collection itself would just be the next new element. 如果你要调用add(Collection) ,那么Collection本身就是下一个新元素。

To call a method that's represented by a Method , call the invoke method . 要调用由Method表示的Method ,请invoke方法 Careful, both the getMethod and invoke methods throw their own Exception s that you'll have to catch. 小心, getMethodinvoke方法都抛出了你自己必须捕获的Exception

You get Method objects by using Reflection on a class object. 通过在类对象上使用Reflection来获取Method对象。

And then you call them by supplying the object you wish them to act on, plus any parameters you wish to supply. 然后通过提供您希望他们操作的对象以及您希望提供的任何参数来调用它们。

Probably a good idea to read the reflection tutorial : http://docs.oracle.com/javase/tutorial/reflect/ 阅读反思教程可能是一个好主意: http//docs.oracle.com/javase/tutorial/reflect/

You can get a method declared in your class like this: 你可以得到一个在你的类中声明的方法,如下所示:

public class MyClass {
    public void a() {
        System.out.println("A!");
    }

    public static void main(String... args)
    {
        Method methodA = getClass().getDeclaredMethod("a");
    }
}
  • You need to have the return type for all the methods (except for constructors) like public static void b (){} . 您需要为所有方法(构造函数除外)提供返回类型,例如public static void b (){}
  • lst.add(1, a()); will just call the method and try to add the return value of the method to the list at a given index. 将只调用该方法并尝试将该方法的返回值添加到给定索引处的列表中。
  • Also, you need to follow the java naming conventions (like class name should always starts with capital) 此外,您需要遵循Java命名约定(类名应始终以大写开头)

If at all you want to add the Method you need to use reflection list.add(a.class.getMethod("b")); 如果你想要添加Method你需要使用反射list.add(a.class.getMethod("b"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM