search

Home  >  Q&A  >  body text

java - 子类重写的方法和父类重写的方法,两个之间的调用

1.当子类对象调用重写的方法时,调用的是子类的方法,而不是父类中被重写的方法
要想调用父类中被重写的方法,则必须使用关键字super。
这句话该怎么理解呢?子类对象调用重写方法,

public class Test {
    public static void main(String[] args) {
      show(new Cat());  // 以 Cat 对象调用 show 方法
      show(new Dog());  // 以 Dog 对象调用 show 方法
                
      Animal a = new Cat();  // 向上转型  
      a.eat();               // 调用的是 Cat 的 eat
      Cat c = (Cat)a;        // 向下转型  
      c.work();        // 调用的是 Cat 的 catchMouse
  }  
            
    public static void show(Animal a)  {
      a.eat();  
        // 类型判断
        if (a instanceof Cat)  {  // 猫做的事情 
            Cat c = (Cat)a;  
            c.work();  
        } else if (a instanceof Dog) { // 狗做的事情 
            Dog c = (Dog)a;  
            c.work();  
        }  
    }  
}
 
abstract class Animal {  
    abstract void eat();  
}  
  
class Cat extends Animal {  
    public void eat() {  
        System.out.println("吃鱼");  
    }  
    public void work() {  
        System.out.println("抓老鼠");  
    }  
}  
  
class Dog extends Animal {  
    public void eat() {  
        System.out.println("吃骨头");  
    }  
    public void work() {  
        System.out.println("看家");  
    }  
}
这里的子对象cat和dog,调用的都是父类的eat。怎么说“当子类对象调用重写的方法时,调用的是子类的方法,而不是父类中被重写的方法”?这话怎么理解呢?
要想调用父类中被重写的方法,则必须使用关键字super
迷茫迷茫2802 days ago896

reply all(1)I'll reply

  • 怪我咯

    怪我咯2017-04-18 10:51:04

    You do not use super in this example. It is recommended to implement the eat method of the parent class and then use super.eat();

    in the subclass.
    1. "When a subclass object calls an overridden method, the method of the subclass is called, not the overridden method in the parent class"
      You should know about polymorphism. The reference of the parent class points to the instance of the subclass . Because the final instance is a subclass, the method of the subclass is called.

    But what should I do if I want to call the method (eat) of the parent class? Then use super
    2. How to use super
    For example:

    public class Test1 {
            public static void main(String[] args) {
                show(new Cat());  // 以 Cat 对象调用 show 方法
                show(new Dog());  // 以 Dog 对象调用 show 方法
    
                Animal a = new Cat();  // 向上转型
                a.eat();               // 调用的是 Cat 的 eat
                Cat c = (Cat)a;        // 向下转型
                c.work();        // 调用的是 Cat 的 catchMouse
            }
    
            public static void show(Animal a)  {
                a.eat();
                // 类型判断
                if (a instanceof Cat)  {  // 猫做的事情
                    Cat c = (Cat)a;
                    c.work();
                } else if (a instanceof Dog) { // 狗做的事情
                    Dog c = (Dog)a;
                    c.work();
                }
            }
        }
    
        abstract class Animal {
            ** void eat(){
                 System.out.println("父类eat...");
             }
        }
    
        class Cat extends Animal {
            public void eat() {
                **super.eat();
                System.out.println("吃鱼");
            }
            public void work() {
                System.out.println("抓老鼠");
            }
        }
    
        class Dog extends Animal {
            public void eat() {
                System.out.println("吃骨头");
            }
            public void work() {
                System.out.println("看家");
            }
        }
        
       输出 结果 

    Father type eat...
    Eat fish
    Catch mice
    Eat bones
    Look after house
    Father type eat...
    Eat fish
    Catch mice

    reply
    0
  • Cancelreply