在类上扩展和返回值

时间:2016-07-28 13:02:32

标签: java

我无法从两个子类中获取我的值。 如何从另外两个类返回主程序的值?类具有层次结构,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;
}
}

1 个答案:

答案 0 :(得分:0)

Cat extends Animal表示每个Cat都是Animal,但不是每个Animal都是Cat

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

但即使使用Animal newAnimal = new Cat(name, 0,9),您也无法访问Cat - 方法。您创建的AnimalCat,但是所有编译器都知道newAnimal是您在使用Animal newAnimal时所说的内容 - 它是Animal }。

所以你可能会说你从一开始就使用Cat

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

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

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