用参数运行吗? [英] Runnable with a parameter?

查看:118
本文介绍了用参数运行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个接受参数的Runnable,虽然我知道这样的runnable并不存在。

I have a need for a "Runnable that accepts a parameter" although I know that such runnable doesn't really exist.

这可能指向一个基本的缺陷我的应用程序的设计和/或在我疲惫的大脑中的心理障碍,所以我希望在这里找到一些关于如何完成以下内容的建议,没有违反基本的OO原则:

This may point to fundamental flaw in the design of my app and/or a mental block in my tired brain, so I am hoping to find here some advice on how to accomplish something like the following, without violating fundamental OO principles:

  private Runnable mOneShotTask = new Runnable(String str) {
    public void run(String str) {
       someFunc(str);
    }
  };  

任何想法如何完成上述内容?

Any idea how to accomplish something like the above?

推荐答案

您可以在方法中声明一个类

You can declare a class right in the method

void Foo(String str) {
    class OneShotTask implements Runnable {
        String str;
        OneShotTask(String s) { str = s; }
        public void run() {
            someFunc(str);
        }
    }
    Thread t = new Thread(new OneShotTask(str));
    t.start();
}

这篇关于用参数运行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆