gpt4 book ai didi

java - 通过类扩展和返回值

转载 作者:太空宇宙 更新时间:2023-11-04 12:23:00 25 4
gpt4 key购买 nike

我无法从两个子类中获取我的值。如何从另外两个类将值返回到主程序?类具有层次结构,Cat.java 扩展了 Animal.Java。我能够从 Animal 类中获取值,但不能从扩展的 Cat 类中获取值。我做错了什么?

主程序

 import java.util.*;

public class animalProject {

private static Scanner input = new Scanner(System.in);

public static void main(String[] args) {

System.out.println("Welcome to create your very own animal.");
System.out.println("Start by typing a name for your animal: ");
String name = input.next();

Animal newAnimal = new Animal(name, 0);

System.out.println("New Animal created");

System.out.println("Set state of your animal: [1] Alive. [2] Dead. ");
int status = input.nextInt();
newAnimal.animalState(status);

System.out.println("Print name of your animal? [1] Yes [2] No ");
int answer = input.nextInt();
if (answer == 1) {
newAnimal.getName();
}

System.out.println("Check status of your animal? [1] Yes [2] No");
answer = input.nextInt();
if (answer == 1) {
newAnimal.checkState();
}

System.out.println("Set lifes for your cat: ");
int life = input.nextInt();
// set lifes to lifesBefore(). in Cat.Java

System.out.println("Remove lifes from cat?: [1] Yes [2] No");
while (true) {
life = input.nextInt(); {
// call the method to decrease lifes from Cat.Java
}
if (life == 2){
break;
}
}

System.out.println("Check cats lifes? [1] Yes [2] No");
answer = input.nextInt();
if (answer == 1) {
// return lifes from Cat.java
}

Animal.Java

public class Animal{

protected String name;
protected int status;


public Animal(String animalName, int animalStatus){
name = amimalName;
state = animalStatus;
}

public void getName() {
System.out.println(name);
}

public void setName() {
this.name = name;
}

public void animalState(int status) {
if (status == 1) {
state = 1; // dead
}
else if (status == 2) {
state = 2; // alive
}
else {
System.out.println("Error with setting state.. program closing..");
System.exit(1);
}
}

public void checkState() {
if (state == 1) {
System.out.println("Animal is dead ");
}
else if (state == 2) {
System.out.println("Animal is alive");
}
else {
System.out.println("Unkown input.. program closing..");
System.exit(1);
}
}
}

Cat.Java

public class Cat extends Animal {

private int catLifes;

public Cat(String animalName int animalStatus, int lifes) {
super(animalName, animalStatus);
catLifes = lifes;
}

public void lifesBefore(){
this.catLifes = lifes;
}


public void decreaseLifes() {

for (int i = 0 ; i < catLifes; i++) {
catLifes--;
}
System.out.println("Cat ran out of lifes and is now dead! ");
// set animals state to dead in Animal.Java
}

public int catsLifesAfter(){
return this.catLifes;
}
}

最佳答案

Cat extends Animal 意味着每个 Cat 都是一个 Animal,但并不是每个 Animal 都是一个 Cat

因此,在您的代码中,您确实使用 new Animal(name, 0) 创建了一个动物,因此您得到了一个 Animal 对象,但不是 Cat - 它也可能是海象。

但即使使用 Animal newAnimal = new Cat(name, 0,9) 您也无法访问 Cat 方法。您创建的 Animal 是一个 Cat,好吧,但是编译器对 newAnimal 的了解就是您在使用 Animal newAnimal 时告诉它的 - 它是一个 Animal

因此,您可能会说您从一开始就使用 Cat:

Cat newAnimal = new Cat(name, 0,9);

但是这样你就会失去你想要探索的继承的大部分功能。作为替代方案,可以稍后使用类型检查和强制转换:

if(newAnimal instanceof Cat) {
Cat aCat = (Cat)newAnimal;
/*...*/
aCat.lifesBefore()
/*...*/
}

关于java - 通过类扩展和返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38637205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com