从超类调用子类方法 [英] Calling a subclass method from superclass
问题描述
我正在学习 Java 入门课程,我们刚刚开始学习继承.我正在处理一项任务,要求我们创建一个带有名称和年龄的宠物"超类;和三个子类,每个子类都有自己独特的特征(我选择了狗"、猫"和鸟").在我们构建完所有这些之后,我们将创建一个 Main 类来测试所有内容,这就是我遇到问题的地方.我试图在 Main
中为这些独特的特征调用 get
方法,但它似乎只能找到超类中的方法.
这是主类:
公共类犬舍{公共静态无效主(字符串 [] args){//创建宠物对象宠物猫 = new Cat("Feline", 12, "Orange");宠物狗 = new Dog("Spot", 14, "Dalmation");宠物鸟 = new Bird("羽毛", 56, 12);//打印动物的状态System.out.println("我有一只猫叫" + cat.getName()+ ".他是 " + cat.getAge() + " 岁."+ " 他是 " + cat.getColor()+ "当他说话时,他说" + cat.speak());System.out.println("我也有一只狗叫" + dog.getName()+ ".他是 " + dog.getAge() + " 岁.+ " 他是 " + dog.getBreed()+ " 当他说话时他说 " + dog.speak());System.out.println("最后我有一只名叫的鸟"+ Bird.getName() + ".他是 " +bird.getAge() + " 岁."+ " 他的翼展为 " +bird.getWingspan() + " 英寸.+ " 当他说话时,他说 " +bird.speak());}}
这是我的超类
抽象公共类宠物{私人字符串名称;私人整数年龄;//构造函数公共宠物(字符串 petName,int petAge){this.name = petName;this.age = petAge;}//吸气剂公共字符串 getName() { return(this.name);}public int getAge() { return(this.age);}//二传手public void setName(String nameSet) { this.name = nameSet;}public void setAge(int ageSet) { this.age = ageSet;}//其他方法抽象 public String speak();//toString@覆盖公共字符串 toString() {String answer = "Name:" + this.name + " Age: " + this.age;返回答案;}}
这是其中一个子类(它们看起来都一样,并且有相同的错误)
public class Cat extends Pet {私人字符串颜色;//构造函数public Cat(String petName, int petAge, String petColor) {超级(宠物名称,宠物年龄);this.color = petColor;}//吸气剂公共字符串 getColor() { return(this.color);}//二传手public void setColor(String colorSet) { this.color = colorSet;}//其他方法@覆盖公共字符串说话(){返回喵!";}//toString@覆盖公共字符串 toString() {String answer = "Name:" + super.getName() + " Age: "+super.getAge()+ " 颜色: " + this.color;返回答案;}}
所以发生的事情是我无法通过 main 方法找到 cat.getColor()
方法,或任何其他子类独有的方法.
当你声明一个变量具有超类的类型时,你只能通过该变量访问超类的(公共)方法和成员变量.
>
宠物猫 = new Cat("Feline",12,"Orange");cat.getName();//还行吧cat.getColor();//这不行,getColor() 不在 Pet 中
要访问具体类(在本例中为Cat
)中的方法,您需要将变量声明为派生类
Cat cat = new Cat("Feline",12,"Orange");cat.getName();//好的,getName() 是 Cat(和超类)的一部分cat.getColor();//好的,getColor() 是 Cat 的一部分
或者将其转换为您知道/怀疑是具体类型的类型
宠物猫 = new Cat("Feline",12,"Orange");((Cat)cat).getName();//OK(同上)((Cat)cat).getColor();//现在我们通过猫的玻璃看猫
您甚至可以将这两种方法结合起来:
Pet pet = new Cat("Feline",12,"Orange");Cat cat = (Cat)pet;cat.getName();//好的cat.getColor();//好的
I am in an introductory java course and we just started learning about inheritance. I am working on a task that asks that we create a "Pet" superclass with name and age; and three subclasses, each with their own unique trait (I have chosen "Dog", "Cat", and "Bird"). After we have all these built, we are to create a Main class to test everything, and this is where I am running into problems. I am attempting to call the get
methods for these unique traits within Main
, but it seems to only find methods that are in the superclass.
Here is the Main 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());
}
}
Here is my superclass
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;
}
}
And here is one of the subclasses (they all look the same and are having the same error)
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;
}
}
So what is happening is I can't get the main method to find the cat.getColor()
method, or any of the other ones unique to the subclasses.
When you declare a variable as having the type of the superclass, you can only access (public) methods and member variables of the superclass through that variable.
Pet cat = new Cat("Feline",12,"Orange");
cat.getName(); // this is OK
cat.getColor(); // this is not OK, getColor() is not in Pet
To access the methods in the concrete class (Cat
in this case), you need to either declare the variable as the derived class
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
Or cast it to a type you know/suspect is the concrete type
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
You can even combine the two methods:
Pet pet = new Cat("Feline",12,"Orange");
Cat cat = (Cat)pet;
cat.getName(); // OK
cat.getColor(); // OK
这篇关于从超类调用子类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!