简体   繁体   English

从超类错误中调用子类的重写方法?

[英]Calling a overridden method of subclass from superclass error?

I have following two set of Java code. 我有以下两组Java代码。 The first one works but the second one doesn't? 第一个有效,但第二个无效吗?

package animal;

import javax.swing.JOptionPane;
import java.util.ArrayList;

public class Animal {

    public static void main(String[] args) {
        Animal createAnimals = new Animal();
        createAnimals.printInfo();
        String userInput = createAnimals.userInputHandle();
        Fishes a = new Fishes();

        switch (userInput) {
            case ("shark"):
                a.printInfo();
        }
    }

    private void printInfo() {
        JOptionPane.showMessageDialog(null, "Welcome to Animal Kingdom.");
    }

    private String userInputHandle() {
        String userInput;
        userInput = JOptionPane.showInputDialog("Select animal from the "
                + "following list"
                + "\n1.Dog\n2.Cat\n3.Snake\n4.Frog"
                + "\n5.Human\n6.Shark\n7.Sea Gulls");
        userInput = userInput.toLowerCase();

        return userInput;
    }
}

class Fishes extends Animal {

    public void printInfo() {
        JOptionPane.showMessageDialog(null, "Shark belongs to Fish subclass of Animal kingdom.");

    }
}

and second set of code is 第二组代码是

package animal;

import javax.swing.JOptionPane;
import java.util.ArrayList;

public class Animal {

    public static void main(String[] args) {
        Animal createAnimals = new Animal();
        createAnimals.printInfo();
        String userInput = createAnimals.userInputHandle();

        ArrayList<Animal> animalList = new ArrayList<Animal>();

        animalList.add(new Fishes());

        switch (userInput) {
            case ("shark"):
                animalList.get(0).printInfo();
            case ("sea gulls"):
        }
    }

    private void printInfo() {
        JOptionPane.showMessageDialog(null, "Welcome to Animal Kingdom.");
    }

    private String userInputHandle() {

        String userInput;
        userInput = JOptionPane.showInputDialog("Select animal from the "
                + "following list"
                + "\n1.Dog\n2.Cat\n3.Snake\n4.Frog"
                + "\n5.Human\n6.Shark\n7.Sea Gulls");
        userInput = userInput.toLowerCase();

        return userInput;
    }
}

class Fishes extends Animal {

    public void printInfo() {
        JOptionPane.showMessageDialog(null, "Shark belongs to Fish subclass of Animal kingdom.");

    }
}

Here the overridden method printInfo is not invoked with animalList.get(0).printInfo() instead class animal's method is invoked? 这里重写的方法printInfo不会与animalList.get(0).printInfo()一起调用,而是会调用class animal的方法吗? Why this is happening? 为什么会这样呢?

#printInfoAnimalprivate ,不能被子类覆盖,您应该将其设置为protected

private methods are not overridden in sub-class . 子类中不重写 私有方法。 change 更改

   private void printInfo() { 

to

  public  void printInfo() {

or 要么

  protected  void printInfo() {

In Animal Class . 动物课中

Try casting the object into the subclass ie (Fishes)animalList.get(0).printInfo(); 尝试将对象转换为子类,即(Fishes)animalList.get(0).printInfo();

Note before casting it is always a good idea to check what type an object is using an if statement using the instanceof comparator. 请注意,在强制转换之前,最好使用instanceof比较器使用if语句检查对象的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM