您的位置:首页 > 其它

面向对象之多态学习

2008-09-25 12:00 225 查看
using System;
using System.Collections.Generic;
using System.Text;

namespace duotai
{
public class Animal
{
public virtual void Speak()//通过定义虚方法实现多态
{
}
}

public class Dog:Animal
{
public override void Speak()
{
Console.WriteLine("Break..Break");
}
}

public class Cat : Animal
{
public override void Speak()
{
Console.WriteLine("ummmm....I see a bird");
}
}
public class Bird : Animal
{
public override void Speak()
{
Console.WriteLine("Please don't eat me mister cat");
}
}
class Program
{
static protected void MakeTheAnimalSpeak(Animal animal)
{
animal.Speak();
}

static void Main(string[] args)
{
Dog dog = new Dog();
Cat cat = new Cat();
Bird bird = new Bird();

MakeTheAnimalSpeak(dog);
MakeTheAnimalSpeak(cat);
MakeTheAnimalSpeak(bird);

Console.ReadLine();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: