**结束。**此问题不符合堆栈溢出准则。它目前不接受答案。
**想改进这个问题吗?**更新问题,使其成为堆栈溢出的主题。
上个月关门了。
改进这个问题
我有一个抽象的类名,抽象的哺乳动物是动物类的后代,狮子是哺乳动物的后代。然后我有类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方法和listofaimals方法
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条答案
按热度按时间y4ekin9u1#
创建这些动物时,其成员字段中没有任何值。因此,以后读取时字段值为空。
你的方法可能是