有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

从抽象类继承的类的java对象从getter返回空值

我有一个抽象的类名,抽象的哺乳动物类是动物类的后代,狮子类是哺乳动物的后代。然后我有类ZooManager,我可以添加动物,编辑动物,显示所有动物等通过简单的控制台应用程序。我已将所有实例保存在ArrayList中。我的问题是,当我尝试获取Lion的属性时,例如,我总是得到null。我试图把getter抽象成动物,然后用在Lion中,但没有成功。我可以从ArrayList中按索引删除实例,但不能使用任何getter。我该怎么办?多谢各位

动物类

abstract public class Animal {
    protected String name;
    protected int birth;
    protected float amount;
    protected String food;

    public Animal(String name, int birth, float amount, String food) {
        this.name = name;
        this.birth = birth;
        this.amount = amount;
        this.food = food;
    }

    public Animal() {

    }

    public abstract String howToFeed();

    public abstract String howToCare();

    public  String getName() {
        return this.name; };

    public int getBirth() {
        return this.birth;
    }

    public float getAmount() {
        return this.amount;
    }

    public String getFood() {
        return this.food;
    }
}

狮子班

 public class Lion extends Mammal {

    public Lion(String name, int birth, float amount, String food) {
        super(name, birth, amount, food);
    }

    public Lion() {
        super();
    }
    
    public String howToFeed() {
        return "Lion " + name + " needs " + amount + " kg of " + food + " per day";
    }

    public String howToCare() {
        return "Lion is an animal";
    }


    @Override
    public String toString() {
        return "Lion{" +
                "name='" + name + '\'' +
                ", birth=" + birth +
                ", amount=" + amount +
                ", food='" + food + '\'' +
                '}';
    }
}

这是ZooManager课程。我要说的部分是removeAnimal方法和listOfAnimals方法

 import java.util.Scanner;
import java.util.ArrayList;

public class ZooManager {

    public ArrayList<Animal> animals;

    public ZooManager() {
        this.animals = new ArrayList<Animal>();
    }

    Scanner sc = new Scanner(System.in);



    private static void mainMenu() {
        System.out.println("------------------ ZOO ------------------");
        System.out.println("1) Add an new animal");
        System.out.println("2) Remove an existing animal");
        System.out.println("3) Show all animals");
        System.out.println("4) Edit animal");
        System.out.println("5) Show animal functions ");
        System.out.println("6) End manager");
        System.out.println("------------------ ZOO ------------------");

    }

    private static void groupMenu(){
        System.out.println("1) Mammal");
        System.out.println("1) Fish");
        System.out.println("1) Bird");
    }

    private static void animalMenu(int group){

        if (group == 1) {
            System.out.println("1) Lion");
            System.out.println("2) Bear");
        } else if (group == 2){
            System.out.println("1) Salmon");
            System.out.println("2) Goldfish");
        } else {
            System.out.println("1) Eagle");
            System.out.println("2) Owl");

        }

    }

    private void addAnimal() {
        Animal[][] className = {{new Lion()}, {new Eagle()}};
        groupMenu();
        System.out.println("Choose a group: ");
        int group = sc.nextInt();
        animalMenu(group);
        int animal = sc.nextInt();
        System.out.println("Name: ");
        sc.nextLine();
        String nameX = sc.nextLine();
        System.out.println("Year of birth: ");
        int birthX = sc.nextInt();
        System.out.println("Amount of food per day: ");
        float amountX = sc.nextFloat();
        System.out.println("Type of food: ");
        sc.nextLine();
        String foodX = sc.nextLine();
        animals.add(className[group - 1][animal - 1]);
        System.out.println(nameX);
        manager();

    }

    private void removeAnimal() {
        System.out.println("Type index of animal you want to remove");
        int index = sc.nextInt();
        String nameX = animals.get(index).getName();
        animals.remove(index);
        manager();
    }

    public void manager(){
        mainMenu();
        int input = sc.nextInt();
        if (input == 1) {
            addAnimal();
        } else if (input == 2){
            removeAnimal();
        } else if (input == 3) {
            listOfAnimals();
        } else if (input == 4) {
            editAnimal();
        } else {
            System.out.println("Ending manager");
        }

    }

    private void listOfAnimals(){
        System.out.println(" index    |name    |birth    |amount    |food");
        for (Animal item: animals) {
            System.out.println(animals.indexOf(item) + "    |" + item.getName() + "    |"  + item.getBirth()  + "    |" + item.getBirth()  + "    |" +  item.getFood());
        }
        manager();
    }

    private void editAnimal(){
        System.out.println("Index of animal u want to edit: ");
        int index = sc.nextInt();
        sc.nextLine();
        System.out.println("1) Name");
        System.out.println("2) Birth");
        System.out.println("3) Amount");
        System.out.println("4) Food");
        System.out.println("Type number: ");
        int input = sc.nextInt();
        sc.nextLine();
        switch (input) {
            case 1 -> {
                System.out.println("Type new name: ");
                animals.get(index).name = sc.nextLine();
            }
            case 2 -> {
                System.out.println("Type new year of birth: ");
                animals.get(index).birth = sc.nextInt();
            }
            case 3 -> {
                System.out.println("Type new amount: ");
                animals.get(index).amount = sc.nextFloat();
            }
            case 4 -> {
                System.out.println("Type new food: ");
                animals.get(index).food = sc.nextLine();
            }
        }
        System.out.println("done");
        manager();
    }



}

更新:这里是哺乳动物类,但现在它只是动物的复制品

abstract public class Mammal extends Animal{

    public Mammal(String name, int birth, float amount, String food) {
        super(name, birth, amount, food);
    }

    public Mammal() {

    }


}

共 (1) 个答案

  1. # 1 楼答案

    *编辑 问题就在这里

     private void addAnimal() {
        Animal[][] className = {{new Lion()}, {new Eagle()}};
    

    创建这些动物时,其成员字段中没有任何值。因此,稍后读取时,字段值为空

    你的方法可能是

     private void addAnimal() {
        groupMenu();
        System.out.println("Choose a group: ");
        int group = sc.nextInt();
        animalMenu(group);
        int animal = sc.nextInt();
        System.out.println("Name: ");
        sc.nextLine();
        String nameX = sc.nextLine();
        System.out.println("Year of birth: ");
        int birthX = sc.nextInt();
        System.out.println("Amount of food per day: ");
        float amountX = sc.nextFloat();
        System.out.println("Type of food: ");
        sc.nextLine();
        String foodX = sc.nextLine();
        Animal[][] className = {{new Lion(namex, birthx, amountx, foodx)}, 
                                {new Eagle(namex, birthx, amountx, foodx)}};
        animals.add(className[group - 1][animal - 1]);
        System.out.println(nameX);
        manager();
    
    }