A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 马林贺 中级黑马   /  2012-5-27 16:28  /  1668 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


  1. <P> </P>
复制代码
  Output out=new Output(res);   对象的时候提示错误。
  1. /创建操作共享数据类
  2. class Res
  3. {
  4. String name;
  5. String sex;
  6. }
  7. //创建多线程操作 信息写入类
  8. class Input implements Runnable
  9. {
  10. Res res;
  11. public Input(Res res)
  12. {
  13. this.res=res;
  14. }
  15. public void run()
  16. { boolean faig=true;
  17. while(true)
  18. {
  19. if(faig)
  20. {
  21. this.res.name="man";
  22. this.res.sex="man";
  23. faig=false;
  24. }
  25. else
  26. {
  27. this.res.name="中文";
  28. this.res.sex="中文";
  29. faig=true;
  30. }
  31. }
  32. }
  33. //创建多线程操作 信息读取类
  34. class Output implements Runnable
  35. {

  36. Res res;
  37. public Output(Res res)
  38. {
  39. this.res=res;
  40. }
  41. public void run()
  42. {
  43. System.out.print(this.res.name+" "+this.res.sex);
  44. }

  45. }
  46. }
  47. class Tongxun {

  48. public static void main(String[] args) {

  49. Res res=new Res();

  50. Input in=new Input(res);

  51. Output out=new Output(res); //这里提示错误了,帮忙解决下


  52. }

  53. }
复制代码

5 个回复

倒序浏览
//创建操作共享数据类
class Res {
        String name;
        String sex;
}

// 创建多线程操作 信息写入类
class Input implements Runnable {
        Res res;

        public Input(Res res) {
                this.res = res;
        }

        public void run() {
                boolean faig = true;
                while (true) {
                        if (faig) {
                                this.res.name = "man";
                                this.res.sex = "man";
                                faig = false;
                        } else {
                                this.res.name = "中文";
                                this.res.sex = "中文";
                                faig = true;
                        }
                }
        }
}

        // 创建多线程操作 信息读取类
        class Output implements Runnable {

                Res res;

                public Output(Res res) {
                        this.res = res;
                }

                public void run() {
                        System.out.print(this.res.name + " " + this.res.sex);
                }

        }


class Test {

        public static void main(String[] args) {

                Res res = new Res();

                Input in = new Input(res);

                Output out = new Output(res);

        }

}
行了
回复 使用道具 举报
黑马11期李项京 发表于 2012-5-27 16:35
//创建操作共享数据类
class Res {
        String name;

  是我的代码写错了吗?
回复 使用道具 举报
求解,我哪里为什么会错呢?
回复 使用道具 举报
马林贺 发表于 2012-5-27 17:04
求解,我哪里为什么会错呢?

Class Out最尾的大括号移到,Class Inter最尾就行了
回复 使用道具 举报
黑马11期李项京 发表于 2012-5-27 18:14
Class Out最尾的大括号移到,Class Inter最尾就行了

知道了。看来半天,我把它弄成内部类了。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马