猎豹扩展程序将无法运行。它给出了所有的狮子输出。但是猎豹不会输出。它运行正常,没有任何错误,只是绕过了猎豹的继承,不会输出它。

修改此任务的现有Animal.java文件。


创建一个名为“猎豹”的类,该类是:
继承自Animal类。
利用至少一个静态字段,该字段需要一个静态的setter和getter。
包含一个构造函数。
包含一个toString()方法。
将数组作为其字段之一。
创建一个应用程序类,并在其中创建一个Cheetah对象,并使用main方法将其打印出来。


所有的一切都在那里,猎豹就不会跑了。

    public class Animal {
    private int numTeeth = 0;
    private boolean spots = false;
    private int weight = 0;

    public Animal(int numTeeth, boolean spots, int weight){
        this.setNumTeeth(numTeeth);
        this.setSpots(spots);
        this.setWeight(weight);
    }

    public int getNumTeeth(){
        return numTeeth;
    }

    public void setNumTeeth(int numTeeth) {
        this.numTeeth = numTeeth;
    }

    public boolean getSpots() {
        return spots;
    }

    public void setSpots(boolean spots) {
        this.spots = spots;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public static void main(String[] args){
        Lion lion = new Lion(30, false, 80);
        System.out.println(lion);
    }

    public static void main1(String[] args){
        Cheetah cheetah = new Cheetah(30, true, 45);
        System.out.println(cheetah);
    }

}


狮子班

 class Lion extends Animal {
    String type = "";

    public Lion(int numTeeth, boolean spots, int weight) {
        super(numTeeth, spots, weight);
        type(weight);
    }
    public String type(int weight){
        super.setWeight(weight);
        if(weight <= 80){
            type = "Cub";
        }
        else if(weight <= 120){
            type = "Female";
        }
        else{
            type = "Male";
        }
        return type;
    }
    @Override
    public String toString() {
        String output = "Number of Teeth: " + getNumTeeth();
        output += "\nDoes it have spots?: " + getSpots();
        output += "\nHow much does it weigh: " + getWeight();
        output += "\nType of Lion: " + type;
        return output;
    }
}


猎豹类:

 class Cheetah extends Animal {

        public Cheetah(int numTeeth, boolean spots, int weight) {
            super(numTeeth, spots, weight);
        }

        public String toString(String cheetah) {
            String output = "Number of Teeth: " + getNumTeeth();
            output += "\nDoes it have spots?: " + getSpots();
            output += "\nHow much does it weigh: " + getWeight();
            output += "\nCheetah";
            return output;
 }
 }

最佳答案

从您的代码中删除main1方法,然后像这样编辑main方法:

public static void main(String[] args){Lion lion = new Lion(30, false, 80);System.out.println(lion);Cheetah cheetah = new Cheetah(30, true, 45);System.out.println(cheetah);}

10-02 06:02