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

继承 通过虚方法实现多态

时间:2014-12-17 20:38:18      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   ar   io   color   os   sp   for   

多态:即多种表现形态

首先定义一个Person父类

bubuko.com,布布扣
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp
{
    /// <summary>
    /// 父类
    /// </summary>
    public class Person
    {
        /// <summary>
        /// 姓名
        /// </summary>
        public string Name
        {
            get;
            set;
        }

        public Person(string name)
        {
            this.Name = name;
        }

        public virtual void Show()
        {
            Console.WriteLine("父类中的方法");
        }
    }
}
View Code

然乎建立几个子类继承自父类

bubuko.com,布布扣
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp
{
    /// <summary>
    /// 中国人类
    /// </summary>
    public class Chinese : Person
    {
        public Chinese(string name)
            : base(name)
        { 
        
        }

        public override void Show()
        {
            Console.WriteLine("我是{0},我是中国人", Name);
        }
    }
}
View Code
bubuko.com,布布扣
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp
{
    public class Japanese : Person
    {
        public Japanese(string name)
            : base(name)
        { 
        
        }

        public override void Show()
        {
            Console.WriteLine("我是{0},我是日本人", Name);
        }
    }
}
View Code
bubuko.com,布布扣
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp
{
    public class American : Person
    {
        public American(string name)
            : base(name)
        {

        }

        public override void Show()
        {
            Console.WriteLine("我叫{0},我是美国人", Name);
        }
    }
}
View Code

效果

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

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Chinese ch1 = new Chinese("孙磊");
            Chinese ch2 = new Chinese("大海");
            Japanese jp1 = new Japanese("安倍");
            Japanese jp2 = new Japanese("苍井空");
            American am1 = new American("奥巴马");
            American am2 = new American("Microsoft");

            Person[] per = new Person[] { ch1, ch2, jp1, jp2, am1, am2 };

            for (int i = 0; i < per.Length; i++)
            {
                #region MyRegion
                //if (per[i] is Chinese)
                //{
                //    Chinese ch = (Chinese)per[i];
                //    ch.Show();
                //}
                //else if (per[i] is Japanese)
                //{
                //    Japanese jp = (Japanese)per[i];
                //    jp.Show();
                //}
                //else if (per[i] is American)
                //{
                //    American am = (American)per[i];
                //    am.Show();
                //}
                #endregion
                per[i].Show();
            }

            Console.ReadKey();
        }
    }
}

bubuko.com,布布扣

 

继承 通过虚方法实现多态

标签:style   blog   http   ar   io   color   os   sp   for   

原文地址:http://www.cnblogs.com/liuhuanping/p/4170305.html

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