再看了下上次写的关于继承的说明代码,发现关于静态变量和静态成员函数的继承问题没有考虑清楚。详见下面测试的代码
public class Test {
        public static void main(String[] args){
          //静态变量A.x即是B.x
          A a=new A();
          B b=new B();
          System.out.println("A.x="+a.getX());
          System.out.println("B.x="+b.getX());
          a.addToX(1);
          System.out.println("A.x="+a.getX());
          System.out.println("B.x="+b.getX());
          b.addToX(1);
          System.out.println("A.x="+a.getX());
          System.out.println("B.x="+b.getX());
          //同样也公用静态方法          
          A.getOpsX();
          B.getOpsX();        
          //静态变量的可见度和静态函数的可见度都不变
          System.out.println("A.x="+A.x);
          System.out.println("B.x="+B.x);
                
        }

}

class A {
        public static int x=2; //1.        
        public static void getOpsX(){
          System.out.println("in A. x*2="+x*2);
        }
        A(){                                          

        }
        public int getX(){
                return x;
        }
        public void addToX(int i){
          System.out.println("in A. add int i="+i+" to X");
          x+=i;
        }

}

class B extends A {
        
        B(){
        }
        public int getX(){
                return x;
        }

        public void addToX(int i){
          System.out.println("in B. add int i="+i+" to X");
          x+=i;
        }

}

//输出
//A.x=2
//B.x=2
//in A. add int i=1 to X
//A.x=3
//B.x=3
//in B. add int i=1 to X
//A.x=4
//B.x=4
//in A. x*2=8
//in A. x*2=8
//A.x=4
//B.x=4


动态绑定情况,只有普通方法才可以动态绑定
public class Test {
        public static void main(String[] args){
          A a=new B();
          //静态方法和静态变量,不动态绑定。使用引用类型
          System.out.println(a.x);       //warning
          System.out.println(a.getOpsX()); //warning
          //普通变量,不动态绑定。使用引用类型
          System.out.println(a.y);
          //普通方法动态绑定,使用实际类型
          System.out.println(a.getX());    
          System.out.println(a.getY());                    
        }
}

class A {
        A(){}
        public static String x="I'm A!";        
        public static String getOpsX(){
          return "in A.use="+x;
        }
        public String y="In A.y";
        public String getX(){
                return x;
        }
        public String getY(){
          return y;
        }

}

class B extends A {        
        B(){}
        public static String x="I'm B!"; //1.        
        public static String getOpsX(){
          return "in B.use="+x;
        }
        
        public String y="In B.y";
        public String getX(){
                return x;
        }
        public String getY(){
          return y;
        }
        public String getSuperY(){
          return super.getY();
        }
}

//输出
//I'm A!
//in A.use=I'm A!
//------------------
//In A.y
//------------------
//I'm B!
//In B.y