class Animal
{
public void Cry()
{
}
}
class Cat extends Animal
{
public void Cry()
{
System.out.println("I am a cat");
}
}
class Dog extends Animal
{
public void Cry()
{
System.out.println("I am a dog");
}
}
public class Fivth
{
public static void main(String args[])
{
Animal animal;
int a = 10;
while(a-- != 0)
{
if(a % 2 == 0)
{
animal = new Cat();
animal.Cry();
}
else
{
animal = new Dog();
animal.Cry();
}
}
}
}
运行结果: