【问题标题】:Is it possible to call a method from within an ArrayList of type Class?是否可以从 Class 类型的 ArrayList 中调用方法?
【发布时间】:2015-11-14 13:31:25
【问题描述】:

我有一个 public ArrayList<Spheres> sphereList = new ArrayList<Spheres>(10); 类的 ArrayList,我需要从 Spheres 类中调用一个方法来更新 sphereList 中的所有元素。那有可能吗,如果可以,我将如何去做?如果不可能,我应该采取其他方法吗?

【问题讨论】:

  • 你想达到什么目的?你的sphereList 是在哪里定义的,在哪个类中?
  • 您能解释一下从类中调用方法是什么意思吗?或者您最好提供伪代码示例以及您对它的期望。
  • 这和处理语言有关系吗?如果没有,它不应该有processing 标签。

标签: java eclipse methods arraylist processing


【解决方案1】:
sphereList.stream().forEach(x -> x.callMethodFromSphere());

【讨论】:

    【解决方案2】:

    一切皆有可能……

    -您必须将父类中的引用传递到 Spheres 的每个实例中

    -或者使用观察者——可观察的设计模式

    -或者将您的 ArrayList 更改为 LinkedList,在调用方法时更新引用链。

    【讨论】:

      猜你喜欢
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-18
      • 1970-01-01
      • 2015-09-17
      • 2014-04-30
      • 2011-08-01
      相关资源
      最近更新 更多