码迷,mamicode.com
首页 > 其他好文 > 详细

面向对象之多态

时间:2015-08-04 15:26:09      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 面向对象之多态
{
    class Program
    {
        static void Main(string[] args)
        {
            //真的鸭子呱呱叫,木头鸭子吱吱叫,橡皮鸭子唧唧叫
            RealDuck rd = new RealDuck();
            MuDuck md = new MuDuck();
            XpDuck xd = new XpDuck();
            RealDuck[] ducks = { rd, md, xd };
            for (int i = 0; i <ducks.Length; i++)
            {
                ducks[i].Sayhello();
            }
            Console.ReadLine();
        }
        public  class RealDuck
        {
            public virtual void Sayhello()  //注意修饰符 virtual 写在方法前,而不是类前
            { 
            Console.WriteLine("真的鸭子呱呱叫");
            }
        }
        public    class MuDuck:RealDuck
        {
            public override void Sayhello()
            {
                Console.WriteLine("木头鸭子吱吱叫");
            }
        }
        public  class XpDuck:RealDuck
        {
            public override  void Sayhello()
            {
                Console.WriteLine("橡皮鸭子唧唧叫");
            }
        
        }
    }
}

 

面向对象之多态

标签:

原文地址:http://www.cnblogs.com/kangshuai/p/4701974.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!