使用匿名内部类调用start方法

package chapter03;
//类实现接口
public class WD implements Runnable{
//重写接口的方法

@Override
public void run() {
for(int i=0; i<500; i++) {
System.out.println("我是豌豆,发炮弹:" + i);
}

}

}

-------------------

package chapter03;

public class Zombies implements Runnable {

@Override
public void run() {
for(int i=0; i<500; i++) {
System.out.println("我是僵尸,往前走" + i);
}

}

}

----------------------------------

package chapter03;
/**
* 使用线程模拟简单植物大战僵尸
*
*/
public class TestPlantsVSZombies {

public static void main(String[] args) {
// 匿名内部类创建对象和调用方法
new Thread(new WD()).start();

new Thread(new Zombies()).start();
/*
* // 创建豌豆对象 WD wd = new WD(); // 创建僵尸对象 Zombies zombies = new
* Zombies();
*
* // 线程一执行豌豆的攻击操作 Thread t1 = new Thread(wd);
*
* // 线程二执行僵尸的前进操作 Thread t2 = new Thread(zombies);
*
* t1.start(); t2.start();
*/

}

}

猜你喜欢

转载自www.cnblogs.com/Koma-vv/p/9616289.html