jackyrong

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1 在继承中,比如
    class a

     {

      void show() {  }

   }

   class b extends a

 {

   static void show() {} //这里是错误的,因为继承的时候,父类的非静态方法,不能在子类中被覆盖为静态方法.

 

  }

 

2 class a

   {

   int i;

  A(int i)

  {

   this.i=i*2;

   }

}

 

  class B extends  A

  {

   public static void main(String[] args)

  {

     B b=new B(2);

   }

B(int i)

  {

System.out.println(i);

}

  }

 

  其中,本例中由于子类B继承A,在子类的构造函数执行时,如果没在子类中显式指定调用父类的某个构造器,会首先执行父类的无参数构造函数,但A类中没这样的函数,所以发生编译错误.因此可以加上super(2);

 

3

  A  静态方法不能访问实例(非静态)变量
       int x=12;

      static void a()

    {

        输出x;//错误

     }

   B  静态方法不能访问非静态方法
      void go()

     static void domore()

   {

      go();

    }

   C  静态方法能访问景泰方法或变量
       static int x;

     static void a()

      static void b()
       {

         访问x;

         a();

}

 

 4 静态方法必须实现,和abstract是冤家,比如
   static abstract void () {..} 是错误的

5 静态方法中也不能有super关键字,因为super是跟具体的类的实例有关,跟static相矛盾.


   

posted on 2008-08-12 12:11  jackyrong的世界  阅读(565)  评论(0编辑  收藏  举报