【问题标题】:Passing an argument to a method defined inside a method in Java将参数传递给在 Java 方法中定义的方法
【发布时间】:2012-11-20 03:56:07
【问题描述】:

所以情况是这样的:

private void myMethod()
{
    System.out.println("Hello World"); //some code

    System.out.println("Some Other Stuff"); 

    System.out.println("Hello World"); //the same code. 

}

我们不想重复我们的代码。

here 描述的技术效果很好:

private void myMethod()
{
    final Runnable innerMethod = new Runnable()
    {
        public void run()
        {
            System.out.println("Hello World"); 
        }
    };

    innerMethod.run();
    System.out.println("Some other stuff"); 
    innerMethod.run(); 
}

但是如果我想将参数传递给该内部方法怎么办?

例如。

private void myMethod()
{
    final Runnable innerMethod = new Runnable()
    {

        public void run(int value)
        {
            System.out.println("Hello World" + Integer.toString(value)); 
        }
    };

    innerMethod.run(1);
    System.out.println("Some other stuff"); 
    innerMethod.run(2); 
}

给我:The type new Runnable(){} must implement the inherited abstract method Runnable.run()

虽然

private void myMethod()
{
    final Runnable innerMethod = new Runnable()
    {
        public void run()
        {
            //do nothing
        }

        public void run(int value)
        {
            System.out.println("Hello World" + Integer.toString(value)); 
        }
    };

    innerMethod.run(1);
    System.out.println("Some other stuff"); 
    innerMethod.run(2); 
}

给我The method run() in the type Runnable is not applicable for the arguments (int)

【问题讨论】:

  • 好的,我完全不清楚为什么这涉及可运行程序和大概的多线程 - 你能澄清一下吗?
  • 您知道Runnable 是用于multithreading 的预定义接口吗?为什么不创建自己的界面?
  • 这是我发现在 Java 中使用方法中的方法的唯一解决方案。
  • 但是你为什么要方法中的方法呢?
  • Runnable 不是为此目的而设计的 (docs.oracle.com/javase/7/docs/api/java/lang/Runnable.html)。您是否尝试定义自己的界面而不是使用 Runnable ?这样,您将拥有所需的所有纬度。

标签: java methods abstract overriding


【解决方案1】:

不,这不是一个方法,而是一个匿名对象。您可以为该对象创建一个额外的方法。

 Thread thread = new Thread(  new Runnable()
    {
      int i,j;
      public void init(int i, int j)
      {
        this.i = i;
        this.j=j;
      }
    });
thread.init(2,3);
thread.start();

然后将runnable包装在一个线程中,然后调用start!不是run()。 因为你不能调用匿名类的构造函数,正如@HoverCraft 所指出的,你可以扩展一个实现Runnable 的命名类。

public class SomeClass implements Runnable
{
   public SomeClass(int i){ }
}

【讨论】:

  • 或者使用一个非匿名类和一个构造函数来获取感兴趣的参数值。
  • 它有效,但我不确定它是否能长期帮助 OP。为此目的使用 Thread / Runnable 感觉非常错误,更不用说 start() 可能产生的副作用了。
  • @Jerome 不幸的是,OP 没有解释他或她为什么要那样做。
【解决方案2】:

看起来你只想要内部方法。 Java 不允许你拥有它们,所以你描述的Runnable hack 让你可以声明一个内部方法。

但既然你想要更多地控制它,为什么不定义你自己的:

interface Inner<A, B> {
    public B apply(A a);
}

那么你可以说:

private void myMethod(..){ 
    final Inner<Integer, Integer> inner = new Inner<Integer, Integer>() {
        public Integer apply(Integer i) {
            // whatever you want
        }
    };


    // then go:
    inner.apply(1);
    inner.apply(2);

}

或者使用一些提供functor 对象的库。应该有很多。 Apache Commons 有一个你可以使用的 Functor。

【讨论】:

  • public B apply(a A); - 这是一个错字吗?你的意思是public B apply (A a)?另外 - 我认为你在实现中遗漏了一些东西 - 代码给了我The type new InnerMethod(){} must implement the inherited abstract method InnerMethod.apply(Object)
  • 是的,很抱歉。我应该将其标记为未经测试的代码。现已修复。
  • 嗯 - 如果我想创建一个 void 方法,我将如何编写它? (或者传入一个 void 参数?)。
猜你喜欢
  • 2013-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多