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相矛盾.


   

相关文章:

  • 2022-02-03
  • 2021-09-26
  • 2022-01-17
  • 2021-09-14
  • 2021-06-29
  • 2022-12-23
  • 2021-06-30
  • 2022-01-24
猜你喜欢
  • 2022-12-23
  • 2021-11-09
  • 2021-08-07
  • 2021-12-21
  • 2021-09-22
  • 2021-07-15
  • 2021-11-24
相关资源
相似解决方案