问答详情
源自:2-3 Java线程-隋唐演义实战开发---演员简介

请问怎么会提示错误呢?

https://img2.mukewang.com/5cbed6b30001565f06260206.jpg

https://img.mukewang.com/5cbed6b400018e6007580094.jpg

package com.xuanxuan;


public class Actor extends Thread {

public void run() {

System.out.println(getName()+"是一个演员");

int count=0;

boolean keepRuning=true;

while(keepRuning) {

System.out.println(getName()+"登台演出"+(++count));

if(count==100) {

keepRuning=false;

}

if(count%10==0) {

try {

Thread.sleep(2000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

System.out.println(getName()+"演出结束了");

}

class Actress implements Runnable{

public void run() {

System.out.println(Thread.currentThread().getName()+"是一个演员");

int count=0;

boolean keepRuning=true;

while(keepRuning) {

System.out.println(Thread.currentThread().getName()+"登台演出"+(++count));

if(count==100) {

keepRuning=false;

}

if(count%10==0) {

try {

Thread.sleep(2000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

System.out.println(Thread.currentThread().getName()+"演出结束了");

}

}


public static void main(String[] args) {

// TODO Auto-generated method stub

Thread actor=new Actor();

actor.setName("先生");

Thread actressThread=new Thread(new Actress(),"女士");

actor.start();

actressThread.start();

}


}


提问者:qq_慕函数8456880 2019-04-23 17:11

个回答

  • tiger爱小狄
    2019-07-03 09:33:31

    class Actress implements Runnable{

    public void run() {

    System.out.println(Thread.currentThread().getName()+"是一个演员");

    int count=0;

    boolean keepRuning=true;

    while(keepRuning) {

    System.out.println(Thread.currentThread().getName()+"登台演出"+(++count));

    if(count==100) {

    keepRuning=false;

    }

    if(count%10==0) {

    try {

    Thread.sleep(2000);

    } catch (InterruptedException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    }

    }

    System.out.println(Thread.currentThread().getName()+"演出结束了");

    }

    }

    这个class Actress叫做内部类,他和public class Actor是同级的,所以Actress应该放在Actor的大括号的外面。

  • qq_念旧_0
    2019-06-13 15:02:39

    Actress类放外面。不是放在Actor里面的

  • 剑雨319
    2019-04-24 21:48:34

    在你的Actress类前面加个static