输出
!
?
$%$^#$@#%^$^#%*&$%&$%:D

解决方案 »

  1.   

    Sub改成
    class Sub extends Super {
        protected int i = 2;
    }
    呢?:)
      

  2.   

    我在该程序中添了一行,结果发生了令人意想不到的情况,有两处
    1.四个打印语句,只打印三个结果出来(如果调试的话可以看见第三个,在jb9中)
    2.第四个结果竟然是10!
    --------------------------------------
    public class test {    public static void main(String[] args) {
            Sub sub = new Sub();
            Super subSuper = new Sub();
            System.out.println(subSuper.i);
            System.out.println(sub.i);
            sub.plus(1);
            System.out.print(sub.i);
            System.out.println(subSuper.i);
        }
    }class Super {
        protected int i;    void plus(int i) {
            this.i += 1;
        }
    }class Sub extends Super {
        protected int i = 1;
    }
      

  3.   

    第一个0应该是java自动对父类i进行的初始化
    第二个1很正常
    我不明白第三个为什么也是1???
    更不明白为什么还会有10??
      

  4.   

    public class test {    public static void main(String[] args) {
            Sub sub = new Sub();
            Super subSuper = new Sub();
            System.out.println(subSuper.i);
            System.out.println(sub.i);
            sub.plus(8);
            System.out.println(subSuper.i);
            System.out.println(sub.i);
            System.out.println(subSuper.i);
        }
    }class Super {
        protected int i=0;    void plus(int i) {
            System.out.println("this.i "+this.i);
            this.i += 1;
            System.out.println("this.i "+this.i);
        }
    }class Sub extends Super {
        protected int i = 3;
    }
    ------------------------------------
    我将上面程序改了一下,楼上的兄弟应该能看明白了吧?其实那个plus方法只对超类里的i有用,
    this指的是其存在类Super的当前对象,而不是调用类test的当前对象,且基本类型int的变量作为入参传入等于是将其副本传入,原值是不变的。
      

  5.   

    楼上的同志:“而不是调用类test的当前对象”
    我可以告诉你这个程序的运行结果和test类是没有关系的。“且基本类型int的变量作为入参传入等于是将其副本传入”
    说得虽然没错,确实是按值传递,但是这个程序的运行结果和这方面没有关系。“其实那个plus方法只对超类里的i有用”
    哎,有点对了,但是没有说清楚。大家请注意生成的两个对象都是Sub型别的,只不过一个被声明为Super而已,但它仍然是Sub类的对象。建议大家用Debug看一下,就清楚了。请善用开发工具。
      

  6.   

    void plus(int i) {
            this.i += 1;
        }
    这个方法怎么看怎么不顺眼,这个i,和+=1有什么用???
    你用
    void plus(int noti) {
            this.i += 100;
        }
    对结果有什么影响??
      

  7.   

    啊,不好意思,写错了,应该是this.i += i;
      

  8.   

    我懂了
            System.out.println(sub.i);
            sub.plus(77);
            System.out.println(sub.i);
            System.out.println(((Super)sub).i);