private void button4_Click(object sender, EventArgs e) { arrayAnimal = new Animal[5]; arrayAnimal[0] = new Cat("小花"); arrayAnimal[0].ShoutNum = 10; arrayAnimal[1] = new Dog("阿毛"); arrayAnimal[2] = new Dog("小黑"); arrayAnimal[3] = new Cat("娇娇"); arrayAnimal[4] = new Cat("咪咪"); }
public static Animal CreateAnimal(string animalCategory, string animalName,int shoutNumber) { Animal result=null; switch (animalCategory) { case "猫": result = new Cat(animalName); break; case "狗": result = new Dog(animalName); break; case "羊": result = new Sheep(animalName); break; } result.ShoutNum = shoutNumber; return result; }
private void button2_Click(object sender, EventArgs e) { Animal dog = new Dog("旺财"); dog.ShoutNum = 3; MessageBox.Show(dog.Shout()); }