public   class   Test   { 
    public   static   void   main(String[]   args)   { 
      Parent   parent   =   new   Parent(); 
      Parent   child   =   new   Child(); 
      System.out.println(parent.getName()); 
      System.out.println(child.getName()); 
    } 
  }   class   Parent   { 
    public   static   String   getName()   { 
      return   "Parent "; 
    } 
  }   class   Child   extends   Parent   { 
    public   static   String   getName()   { 
      return   "Child "; 
    } 
  }答案是什么,问什么呢?

解决方案 »

  1.   

     public   class   Test   {  
        public   static   void   main(String[]   args)   {  
          Parent   parent   =   new   Parent();  
          Parent   child   =   new   Child();  
          System.out.println(parent.getName());  
          System.out.println(child.getName());  
        }  
      }    class   Parent   {  
        public   String   getName()   {  
          return   "Parent ";  
        }  
      }    class   Child   extends   Parent   {  
        public   String   getName()   {  
          return   "Child ";  
        }  
      } 这个答案是什么,问什么呢? 
      

  2.   

    "Parent"
    "Parent"因为Parent和Child这2个类的getName()方法都是静态的,静态方法是不能被重载的
    main方法里面parent和child这2个对象调用getName()方法时,相当于直接用类名调用,即Parent.getName()
      

  3.   

    "Parent" 
    "Parent" 因为 Parent   child   =   new   Child();   
    这里调用的getname()是调用Parent的getName()  。