多态和反射

一、多态:

(1)接口多态:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Collections;  
  
namespace ConsoleApplication  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            DongWu Dw1 = new Mao();  
            Dw1.Name(); //输出Mao  
            DongWu Dw2 = new Gou();  
            Dw2.Name();//输出Gou  
        }  
    }  
    /// <summary>  
    /// 动物接口  
    /// </summary>  
    public interface DongWu  
    {  
        /// <summary>  
        /// 名字  
        /// </summary>  
        void Name();  
    }  
    /// <summary>  
    /// 猫类  
    /// </summary>  
    public class Mao : DongWu  
    {  
        public void Name()  
        {  
            Console.WriteLine("Mao");  
        }  
    }  
    /// <summary>  
    /// 狗类  
    /// </summary>  
    public class Gou : DongWu  
    {  
        public void Name()  
        {  
            Console.WriteLine("Gou");  
        }  
    }  
  
}  

(2)非抽象非接口多态:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Collections;  
  
namespace ConsoleApplication  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            DongWu Dw = new DongWu();  
            Dw.Name(); //输出DongWu  
            DongWu Dw1 = new Mao();  
            Dw1.Name(); //输出Mao  
            DongWu Dw2 = new Gou();  
            Dw2.Name();//输出Gou  
        }  
    }  
    /// <summary>  
    /// 动物类  
    /// </summary>  
    public class DongWu  
    {  
        /// <summary>  
        /// 名字  
        /// </summary>  
        public virtual void Name()  
        {  
            Console.WriteLine("DongWu");  
        }  
    }  
    /// <summary>  
    /// 猫类  
    /// </summary>  
    public class Mao : DongWu  
    {  
        public override void Name()  
        {  
            Console.WriteLine("Mao");  
        }  
    }  
    /// <summary>  
    /// 狗类  
    /// </summary>  
    public class Gou : DongWu  
    {  
        public override void Name()  
        {  
            Console.WriteLine("Gou");  
        }  
    }  
  
}  

(3)抽象多态:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Collections;  
  
namespace ConsoleApplication  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            DongWu Dw1 = new Mao();  
            Dw1.Name(); //输出Mao  
            DongWu Dw2 = new Gou();  
            Dw2.Name();//输出Gou  
        }  
    }  
    /// <summary>  
    /// 动物类  
    /// </summary>  
    public abstract class DongWu  
    {  
        /// <summary>  
        /// 名字  
        /// </summary>  
        public abstract void Name();  
    }  
    /// <summary>  
    /// 猫类  
    /// </summary>  
    public class Mao : DongWu  
    {  
        public override void Name()  
        {  
            Console.WriteLine("Mao");  
        }  
    }  
    /// <summary>  
    /// 狗类  
    /// </summary>  
    public class Gou : DongWu  
    {  
        public override void Name()  
        {  
            Console.WriteLine("Gou");  
        }  
    }  
  
}  

二、反射

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

namespace 并发和多少线程
{
    class Program
    {
       
        static void Main(string[] args)
        {

            Assembly assembly = Assembly.Load("并发和多少线程"); //程序集名称  
            Type type = assembly.GetType("并发和多少线程.DongWuY");//程序集.类名  
            MethodInfo methodinfo = type.GetMethod("NameHello");//方法名  
            Object obj = assembly.CreateInstance("并发和多少线程.DongWuY");//调用方法  
            methodinfo.Invoke(obj, null);//实现方法  
        }
    }
    /// <summary>  
    /// 动物类  
    /// </summary>  
    public class DongWuY
    {
        /// <summary>  
        /// 名字  
        /// </summary>  
        public void NameHello()
        {
            Console.WriteLine("DongWu");  
        }
    }
}

运行效果:

posted @ 2016-11-30 16:08  狼牙者.net  阅读(263)  评论(0编辑  收藏  举报