class A{ private static num1,num2;
 static List<C> c;  A(){
  c = new ArrayList<C>;
  }  void receive(Mess message){
   if(message instanceof lookup)
             num1++;
   else if(message instanceof takeme)
            num2++;
  }  void write(){
     c.add(new C(num1,num2));
  }  void output(){
   for(C ins: c){
     sysout(ins.getw1() +" " +ins.getw2());
   }
  }  class C{
  int w1,w2;
  C(int w1, int w2){
  this.w1 =w1;
  this.w2=w2;
  }
  /***getter***/
  }
}
由于类A的特殊性  所以必须字段都为static 否则输出结果不对 A内有嵌套类C num1,num2是计数器
这段代码最后会运行output方法 输出在arraylist里面每个index里存着的 w1,w2值 可是非常奇怪的是 w1值正常 w2的值死活都等于0 
我测试过 在receive方法内 num2工作正常  就是说 num2++ 是有运行的 的出来的结果也不等于0 可是为什么作为参数传给C类 就是0呢了 而且怪就怪在 w1值是正常的 

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【iskohl】截止到2008-07-27 02:55:34的历史汇总数据(不包括此帖):
    发帖的总数量:2                        发帖的总分数:40                       每贴平均分数:20                       
    回帖的总数量:1                        得分贴总数量:0                        回帖的得分率:0%                       
    结贴的总数量:1                        结贴的总分数:20                       
    无满意结贴数:0                        无满意结贴分:0                        
    未结的帖子数:1                        未结的总分数:20                       
    结贴的百分比:50.00 %               结分的百分比:50.00 %                  
    无满意结贴率:0.00  %               无满意结分率:0.00  %                  
    楼主加油
      

  2.   

    http://blog.csdn.net/carefree31441先创建个A的对象,通过A得对象创建B的对象,试试看!
      

  3.   

    大家帮帮忙啦 我百思不得其解 太怪了 w1正常 w2不正常 w1,w2都是全局变量啊 而且我把w1注释掉 w2照样是0 
      

  4.   

    不可能把全部完整代码 贴出来的 这里只是出问题的类 大致流程就是由一个xml文件初始化这个类 然后开始运行 receive方法会接受很多消息message 然后判断消息类型 各自计数器每次递增1,而write方法会被定时施行(被其他类引用比如是B 所以这里字段要用static 因为B不能获得xml初始化的实例 所以B只能用new A出来的的实例) 就是记录在那个时间点 有多少lookup 和takeme 消息 最后则是output被运行 输出每个时间点的消息计数器的值我也想过 是不是B类new出来的结果导致w2没有值,但是w1是正常的 这就比较怪了
    谢谢
      

  5.   

    给你一段我的实现代码,能够正确显示。你看看和你的有什么不同。package Test;import java.util.ArrayList;
    import java.util.List;class A {    private static int num1, num2;
        static List<C>     c;    A() {
            c = new ArrayList<C>();
        }    void receive(Mess message) {
            if (message instanceof lookup)
                num1++;
            else if (message instanceof takeme)
                num2++;
        }    void write() {
            c.add(new C(num1, num2));
        }    void output() {
            for (C ins : c) {
                System.out.println(ins.getw1() + " " + ins.getw2());
            }
        }
        
        public static void main(String[] args){
            A a = new A();
            a.receive(new lookup());
            a.receive(new takeme());
            a.write();
            a.output();
        }
        
        class C {        int w1, w2;        C(int w1, int w2) {
                this.w1 = w1;
                this.w2 = w2;
            }        public int getw1() {
                return w1;
            }        public void setw1(int w1) {
                this.w1 = w1;
            }        public int getw2() {
                return w2;
            }        public void setw2(int w2) {
                this.w2 = w2;
            }
        }
    }
    interface Mess {
    }
    class lookup implements Mess {
    }
    class takeme implements Mess {}