码迷,mamicode.com
首页 > Windows程序 > 详细

练习:C# -- 多态

时间:2016-06-19 18:17:47      阅读:325      评论:0      收藏:0      [点我收藏+]

标签:

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

namespace 多态
{
    class Program
    {
        static void Main(string[] args)
        {
            /*
             练习:
             真的鸭子嘎嘎叫
             木头鸭子吱吱叫
             橡胶鸭子唧唧叫
             */
            RealDark rrd = new RealDark();
            WoodenDark wd = new WoodenDark();
            RubberDark rd = new RubberDark();

            Dark[] d = { rrd, wd, rd };
            for (int i = 0; i < d.Length; i++)
            {
                d[i].Bark();
            }
            Console.ReadLine();
        技术分享

            /*
             练习:
             经理10点打卡
             员工9点打卡
             程序员不打卡
             */
            Employee em = new Employee();
            Manager ma = new Manager();
            Programmer pr = new Programmer();
            Employee[] ee = { em, ma, pr };
            for (int i = 0; i < ee.Length; i++)
            {
                ee[i].daka();
            }
            Console.ReadLine();
        技术分享
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 多态
{

    public class Dark
    {
        public virtual void Bark()
        {
            Console.WriteLine("我是鸭子");
        }
    }

    public class RealDark : Dark
    {
        public override void Bark()
        {
            Console.WriteLine("真的鸭子嘎嘎叫~");
        }
    }

    public class WoodenDark : Dark
    {
        public override void Bark()
        {
            Console.WriteLine("木头鸭子吱吱叫~");
        }
    }

    public class RubberDark : Dark
    {
        public override void Bark()
        {
            Console.WriteLine("橡胶鸭子唧唧叫~");
        }
    }



    public class Employee
    {
        public virtual void daka()
        {
            Console.WriteLine("员工9点打卡");
        }
    }

    public class Manager : Employee
    {
        public override void daka()
        {
            Console.WriteLine("经理10点打卡");
        }
    }

    public class Programmer : Employee
    {
        public override void daka()
        {
            Console.WriteLine("程序员不打卡");
        }
    }

}

 

练习:C# -- 多态

标签:

原文地址:http://www.cnblogs.com/xiao55/p/5598397.html

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