📄 trypolymorphism.java
字号:
import java.util.Random;
public class TryPolymorphism
{
public static void main(String[] args)
{
// Create an array of three different animals
Animal[] theAnimals = {
new Dog("Rover", "Poodle"),
new Cat("Max", "Abyssinian"),
new Duck("Daffy","Aylesbury")
};
Animal petChoice; // Choice of pet
Random select = new Random(); // Random number generator
// Make five random choices of pet
for(int i = 0; i < 5; i++)
{ // Choose a random animal as a pet
petChoice = theAnimals[select.nextInt(theAnimals.length)];
System.out.println("\nYour choice:\n" + petChoice);
petChoice.sound(); // Get the pet's reaction
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -