请选择下面这段代码的输出结果? 
class A{ 
   public int getNumber(int a){ 
    return a+1; 
    } 
    } 
   class B extends A{ 
     public int getNumber(int a, char c){ 
     return a+2; 
    } 
    public static void main(String[] args){ 
     B b=new B(); 
     System.out.println(b.getNumber(0)); 
    } 
    }
  A. compilation succeeds and 1 is printed 
  B. compilation succeeds and 2 is printed 
  C. An error at line 8 cause compilation to fail 
  D. An error at line 14 cause compilation to fail

解决方案 »

  1.   

    输出应该是1吧~~B类继承A类,并没有覆盖A类方法
      

  2.   

    可是在B b = new B();这一行前面就有个红叉,不知道啥原因?
    编译后结果是:Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    No enclosing instance of type HelloWorld is accessible. Must qualify the allocation with an enclosing instance of type HelloWorld (e.g. x.new A() where x is an instance of HelloWorld). at com.soft.team.HelloWorld.main(HelloWorld.java:15)
      

  3.   


    目测你的代码是public class HelloWorld{
    class A{ 
      public int getNumber(int a){ 
      return a+1; 
      } 
      } 
      class B extends A{ 
      public int getNumber(int a, char c){ 
      return a+2; 
      } 
      public static void main(String[] args){ 
      B b=new B(); 
      System.out.println(b.getNumber(0)); 
      } 
      }
    }
      

  4.   


    public class HelloWorld{
      public static void main(String[] args){ 
      B b=new B(); 
      System.out.println(b.getNumber(0)); 
      } 
      
    }
    class A{ 
      public int getNumber(int a){ 
      return a+1; 
      } 
      } 
      class B extends A{ 
      public int getNumber(int a, char c){ 
      return a+2; 
      } 
    }关键字
    java 内部类