关于Android如何给传递线程初始化数据

可创建一个内部类实现Runable接口,并设置一个传递数据的方法:


class myRunable implements Runnable {
        private Object enemy;
        private int delay;
        public myRunable(Object e, int d) {
            // TODO Auto-generated constructor stub
            enemy = e;
            delay = d;
                                                                                                                        
        }
        @Override
        public void run() {
            // TODO Auto-generated method stub
            try {
                Thread.sleep(delay);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.print(enemy.toString());
        }

上代码使构造方法传递数据,然后再创建线程即可.

private void Method(Obeject e, int delay) {
    Runnable runnable = new myRunable(e, delay);
    Thread thread = new Thread(runnable);
    thread.start();
}

个人认为,此种方法不光使用于线程初始化,而且在很多有关传递数据的时候,都可采用到.

你可能感兴趣的:(android,object,method,private,public)