public Road(String name) {}是你的构造方法(带了参数的)
this指的是当前类对象,你这段代码里就一个类,没有内部类.而且这个线程都没启动,我改了一下你的代码.- import java.util.LinkedList;
- import java.util.Random;
- public class Road {
- private String name;
- private LinkedList<String> vechicles = new LinkedList<String>();
- public Road(String name) {
- super();
- this.name = name;
- new Thread(new Runnable(){
- public void run(){
- try {
- Thread.sleep((new Random().nextInt(10)+1)*1000);
-
- } catch (Exception e) {
- // TODO: handle exception
- }
- vechicles.add(Road.this.name);
- System.out.println(vechicles.getLast());
- }
- }).start();
- }
- public static void main(String[]args){
- new Road("ha");
- }
- }
复制代码 |