从超类调用子类方法

我在介绍性的java课程,我们刚刚开始学习inheritance。 我正在做一个任务,要求我们创build一个名字和年龄的“宠物”超类; 和三个亚类,每个都有自己独特的特征(我select了“狗”,“猫”和“鸟”)。 所有这些build成之后,我们要创build一个Main类来testing一切,这就是我遇到问题的地方。 我试图在Main调用这些独特特征的get方法,但似乎只能find超类中的方法。

这是主要类:

 public class Kennel { public static void main(String[] args) { // Create the pet objects Pet cat = new Cat("Feline", 12, "Orange"); Pet dog = new Dog("Spot", 14, "Dalmation"); Pet bird = new Bird("Feathers", 56, 12); // Print out the status of the animals System.out.println("I have a cat named " + cat.getName() + ". He is " + cat.getAge() + " years old." + " He is " + cat.getColor() + "When he speaks he says " + cat.speak()); System.out.println("I also have a dog named " + dog.getName() + ". He is " + dog.getAge() + " years old." + " He is a " + dog.getBreed() + " When he speaks he says " + dog.speak()); System.out.println("And Finally I have a bird named " + bird.getName() + ". He is " + bird.getAge() + " years old." + " He has a wingspan of " + bird.getWingspan() + " inches." + " When he speaks he says " + bird.speak()); } } 

这是我的超类

 abstract public class Pet { private String name; private int age; // Constructor public Pet(String petName, int petAge) { this.name = petName; this.age = petAge; } // Getters public String getName() { return(this.name); } public int getAge() { return(this.age); } // Setters public void setName(String nameSet) { this.name = nameSet; } public void setAge(int ageSet) { this.age = ageSet; } // Other Methods abstract public String speak(); // toString @Override public String toString() { String answer = "Name: " + this.name + " Age: " + this.age; return answer; } } 

这里是其中一个子类(它们看起来都是一样的,并且有相同的错误)

 public class Cat extends Pet { private String color; // Constructor public Cat(String petName, int petAge, String petColor) { super(petName, petAge); this.color = petColor; } // Getters public String getColor() { return(this.color); } // Setters public void setColor(String colorSet) { this.color = colorSet; } // Other Methods @Override public String speak() { return "Meow!"; } // toString @Override public String toString() { String answer = "Name: " + super.getName() + " Age: "+super.getAge() + " Color: " + this.color; return answer; } } 

所以发生了什么是我不能得到主要的方法来findcat.getColor()方法,或任何其他独特的子类。

当你声明一个variables为超类的types时,你只能通过该variables访问超类​​的(public)方法和成员variables。

 Pet cat = new Cat("Feline",12,"Orange"); cat.getName(); // this is OK cat.getColor(); // this is not OK, getColor() is not in Pet 

要访问具体类中的方法(在这种情况下是Cat ),您需要将该variables声明为派生类

 Cat cat = new Cat("Feline",12,"Orange"); cat.getName(); // OK, getName() is part of Cat (and the superclass) cat.getColor(); // OK, getColor() is part of Cat 

或者把它转换成你知道/怀疑是具体types的types

 Pet cat = new Cat("Feline",12,"Orange"); ((Cat)cat).getName(); // OK (same as above) ((Cat)cat).getColor(); // now we are looking at cat through the glass of Cat 

你甚至可以结合这两种方法:

 Pet pet = new Cat("Feline",12,"Orange"); Cat cat = (Cat)pet; cat.getName(); // OK cat.getColor(); // OK 

当你这样做:

  Pet cat = new Cat("Feline",12,"Orange"); 

编译器将variablescat看作Pet。 所以你不能使用Cat类的具体方法。

您必须将Cat声明为Type Cat来解决您的问题。

问候。

 Pet cat = new Cat("Feline",12,"Orange"); ^^^ This is the error. 

Pet没有一个名为getColor()的方法

你需要做的是:

 Cat cat = new Cat(...); 

在这里,您正在尝试使用构造函数(Cat)创build超types对象(cat),这是子类方法,这是不可能的。 这是违反inheritance的规则。

Pet cat = new Cat("Feline", 12, "Orange");