Java 多态性 - 引用变量问题
Java polymorphism - Reference variables issue
我正在学习java和多态。我正在使用 java 开头的书。我试图用它们做实验。我了解到您可以调用的方法取决于参考 object.So 我创建了这段代码
package animals;
public class AnimalstestDrive {
static public Animals[] myZoo = new Animals[5];
static int zooCounter = 0;
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Dog 1 test");
Dog myDog = new Dog();
myDog.eatFood();
myDog.sleep();
myDog.makeNoises();
System.out.println("Dog 2 test");
Animals myNewDog = new Dog();
myNewDog.eatFood();
myNewDog.makeNoises();
//Set animals array
System.out.println("Dog 3 test");
Animals zooDog = new Dog();
addAnimals(zooDog);
Cat zooCat = new Cat();
addAnimals(zooCat);
myZoo[0].makeNoises();
}
public static void addAnimals(Animals a){
if ( zooCounter < 5 ){
myZoo[zooCounter] = a;
zooCounter++;
}
else
System.out.println("Zoo is full");
}
}
导致我出现问题的一件事是 "Dog 3 test" 没有显示在控制台上。 IT 在 Dog 2 之前工作正常,但 Dog 3 不工作。
这是我的动物class
package animals;
public abstract class Animals {
private String Name;
private int Size; //Size on the scale 1 to 10
public void eatFood(){
System.out.println("I am eating food");
}
public void sleep(){
System.out.println("I am sleeping now");
}
abstract public void makeNoises();
}
我认为这是一个打字错误,因为您在一个地方使用 myNewDog.makeNoises()
而在另一个地方使用 myZoo[0].makeNoise();
因此,如果它是错字 将其更改为 myZoo[0].makeNoises();
关于 upcasting and downcasting
见 What is the difference between up-casting and down-casting with respect to class variable:
Up-casting is casting to a supertype, while downcasting is casting to a subtype.
因此,当您键入 Animals zooDog = new Dog();
时,它是一个向上转换。
并且 Dog newDog = (Dog)zooDog;
这是一个沮丧。
有关详细信息,请参阅 JLS 5.5.1. Reference Type Casting
我正在学习java和多态。我正在使用 java 开头的书。我试图用它们做实验。我了解到您可以调用的方法取决于参考 object.So 我创建了这段代码
package animals;
public class AnimalstestDrive {
static public Animals[] myZoo = new Animals[5];
static int zooCounter = 0;
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Dog 1 test");
Dog myDog = new Dog();
myDog.eatFood();
myDog.sleep();
myDog.makeNoises();
System.out.println("Dog 2 test");
Animals myNewDog = new Dog();
myNewDog.eatFood();
myNewDog.makeNoises();
//Set animals array
System.out.println("Dog 3 test");
Animals zooDog = new Dog();
addAnimals(zooDog);
Cat zooCat = new Cat();
addAnimals(zooCat);
myZoo[0].makeNoises();
}
public static void addAnimals(Animals a){
if ( zooCounter < 5 ){
myZoo[zooCounter] = a;
zooCounter++;
}
else
System.out.println("Zoo is full");
}
}
导致我出现问题的一件事是 "Dog 3 test" 没有显示在控制台上。 IT 在 Dog 2 之前工作正常,但 Dog 3 不工作。
这是我的动物class
package animals;
public abstract class Animals {
private String Name;
private int Size; //Size on the scale 1 to 10
public void eatFood(){
System.out.println("I am eating food");
}
public void sleep(){
System.out.println("I am sleeping now");
}
abstract public void makeNoises();
}
我认为这是一个打字错误,因为您在一个地方使用 myNewDog.makeNoises()
而在另一个地方使用 myZoo[0].makeNoise();
因此,如果它是错字 将其更改为 myZoo[0].makeNoises();
关于 upcasting and downcasting
见 What is the difference between up-casting and down-casting with respect to class variable:
Up-casting is casting to a supertype, while downcasting is casting to a subtype.
因此,当您键入 Animals zooDog = new Dog();
时,它是一个向上转换。
并且 Dog newDog = (Dog)zooDog;
这是一个沮丧。
有关详细信息,请参阅 JLS 5.5.1. Reference Type Casting