我正在学习 Java 入门类(class),我们刚刚开始学习继承。我正在处理一项任务,要求我们创建一个带有名称和年龄的“宠物”父类(super class);和三个子类,每个子类都有自己独特的特征(我选择了“狗”、“猫”和“鸟”)。在我们构建完所有这些之后,我们将创建一个 Main 类来测试所有内容,这就是我遇到问题的地方。我试图在 Main
中为这些独特的特征调用 get
方法,但它似乎只能找到父类(super class)中的方法。
这是主类:
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());
}
}
这是我的父类(super class)
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;
}
}
所以发生的事情是我无法通过 main 方法找到 cat.getColor()
方法,或任何其他子类独有的方法。
最佳答案
当你声明一个变量具有父类(super class)的类型时,你只能通过这个变量访问父类(super class)的(公共(public))方法和成员变量。
Pet cat = new Cat("Feline",12,"Orange");
cat.getName(); // this is OK
cat.getColor(); // this is not OK, getColor() is not in Pet
要访问具体类(本例中为 Cat
)中的方法,您需要将变量声明为派生类
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
或将其转换为您知道/怀疑是具体类型的类型
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
关于java - 从父类(super class)调用子类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10021603/