下面的哪个选项是正确的?  
class ExSuper{   
   String name;   
   String nick_name;   
      public ExSuper(String s,String t){    
            name = s;    
            nick_name = t;   
     }    
       public String toString(){     
          return name;    
      }   
      }   
       public class Example extends ExSuper{    
            public Example(String s,String t){    
                 super(s,t);    
                }    
           public String toString(){     
               return name +"a.k.a"+nick_name;    
              }    
           public static void main(String args[]){                      ExSuper a = new ExSuper("First","1st");                      ExSuper b = new Example("Second","2nd");                System.out.println("a is"+a.toString());                  System.out.println("b is"+b.toString());    
      }  
  }
 A 编译时会出现例外。
 B 运行结果为: a is First     b is second
 C 运行结果为: a is First     b is Secong a.k.a 2nd
 D 运行结果为: a is First a.k.a 1nd  b is Second a.k.a 2nd

解决方案 »

  1.   

    Example构造调用了父类的构造,所以那两个变量时有值的。
      

  2.   

    当new ExSuper("First","1st");调用ExSuper的构造函数
     public Example(String s,String t){
    super(s,t); 
    }
    当super(s,t)是调用父类
      

  3.   

    ExSuper a = new ExSuper("First","1st");          ExSuper b = new Example("Second","2nd");  你吗的new 的二个对象都不同 啃D
      

  4.   

    题目想要让你选的是C(考察多态)
    但实际上,打印的应该是
    a isFirst
    b isSeconda.k.a2nd
      

  5.   

    ExSuper b = new Example("Second","2nd");  
    这个里面 类型虽然是ExSuper,但对象的实际类型还是Example,若Example里面的方法和域多于ExSuper ,则b就调用不了那些新增的方法和域了
      

  6.   

    父子类的关系 ,还有和super();有关    
      

  7.   

    答案就是C啦那个b很明显是对象new Example(……)的对象么!!
      

  8.   

    说错了,是new Example()的引用。
      

  9.   

    不要急着选哪个,重要是搞清楚问题,
      ExSuper a = new ExSuper("First","1st");
      ExSuper b = new Example("Second","2nd"); 
    第一句new ExSuper,toString()-->return name;第二句new Example,toString()-->return name +"a.k.a"+nick_name,所以选 C 。
      

  10.   

    我是楼主,为何会出现打印结果是“a is First  b is Secong a.k.a 2nd”,而不是("First","1st"); ("Second","2nd");
      

  11.   

    偶看懂了,因为调用toString()方法