关闭

c#关于接口的代码说明,请认真对比

342人阅读 评论(0) 收藏 举报

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            PrintFactory myFactory = new PrintFactory();
            Iprint myPrint = myFactory.CreatePrint();
            System.Console.WriteLine(myPrint.PrintData("这样做很方便啊"));
            System.Console.WriteLine("-----以下使用接口数组--------------");
            Iprint[] list = new Iprint[6];
            list[0] = new PrintA();
            list[1] = new PrintB();
            list[2] = new PrintC();
            list[3] = new PrintC1();
            list[4] = new PrintD();
            list[5] = new PrintE();

            for (int i = 0; i < list.Length; i++)
                System.Console.WriteLine(list[i].PrintData(i.ToString()));

            System.Console.WriteLine("-----以下使用ArrayList--------------");
            ArrayList arraylist = new ArrayList();
            arraylist.Add(new PrintA());
            arraylist.Add(new PrintB());
            arraylist.Add(new PrintC());
            arraylist.Add(new PrintC1());
            arraylist.Add(new PrintD());
            arraylist.Add(new PrintE());

            for (int i = 0; i < arraylist.Count; i++)
            {
                System.Console.WriteLine(((Iprint)arraylist[i]).PrintData(i.ToString()));
            }
            System.Console.WriteLine("===如果没有接口的话,在循环中必须用以上六个类的变量来分别获取数据元素==========");
            System.Console.WriteLine("-----lei--------------");
            (new BClass()).Run();
            (new FClass()).Run();
            ((BClass)(new FClass())).Run();
            System.Console.WriteLine("-----virtual--------------");
            (new BvirtualClass()).Run();
            (new FvirtualClass()).Run();
            (new F1virtualClass()).Run();
            ((BvirtualClass)(new FvirtualClass())).Run();
            ((BvirtualClass)(new F1virtualClass())).Run();
           
        }
    }
   
    interface   Iprint{  
        string PrintData(string   data);  
    }  


    class   PrintA:Iprint{  
      public virtual string PrintData(string   data)
      {
          System.Console.WriteLine("发生在:PrintA");
          return "PrintA:" + data;
      }  
    }

    class   PrintB:Iprint{  
      public   virtual  string    PrintData(string   data)
      {
          System.Console.WriteLine("发生在:PrintB");
          return "PrintB:" + data;
      }  
    }
    class PrintC : PrintB
    {
        public virtual string PrintData(string data)
        {
            System.Console.WriteLine("发生在:PrintC");
            return "PrintC:" + data;
        }
    }
    class PrintC1 : PrintB
    {
        public string PrintData(string data)
        {
            System.Console.WriteLine("发生在:PrintC1");
            return "PrintC1:" + data;
        }
    }

    class PrintD : PrintB
    {
        public override string PrintData(string data)
        {
            System.Console.WriteLine("发生在:PrintD");
            return "PrintD:" + data;
        }
    }
    class PrintE : PrintB, Iprint
    {
        public override string PrintData(string data)
        {
            System.Console.WriteLine("发生在:PrintE");
            return "PrintE:" + data;
        }
    }

    //定义工厂类  
    class   PrintFactory{  
      public   Iprint   CreatePrint()
      {  
          return   new   PrintA();  
      }  
    }  

    /////
    class BClass {
        public void Run()
        {
            System.Console.WriteLine("发生在:BClass");
        }
    }
    class FClass:BClass
    {
        //new public void  Run() 这样写,表明真实覆盖
        public void  Run()
        {
            System.Console.WriteLine("发生在:FClass");
        }
    }
    ///
    class BvirtualClass
    {
        public virtual void Run()
        {
            System.Console.WriteLine("发生在:BClass");
        }
    }
    class FvirtualClass : BvirtualClass
    {
        public override void Run()
        {
            System.Console.WriteLine("发生在:FClass");
        }
    }
    class F1virtualClass : BvirtualClass
    {
        public virtual void Run()
        {
            System.Console.WriteLine("发生在:F1Class");
        }
    }   
}

 


运行结果:

发生在:PrintA
PrintA:这样做很方便啊
-----以下使用接口数组--------------
发生在:PrintA
PrintA:0
发生在:PrintB
PrintB:1
发生在:PrintB
PrintB:2
发生在:PrintB
PrintB:3
发生在:PrintD
PrintD:4
发生在:PrintE
PrintE:5
-----以下使用ArrayList--------------
发生在:PrintA
PrintA:0
发生在:PrintB
PrintB:1
发生在:PrintB
PrintB:2
发生在:PrintB
PrintB:3
发生在:PrintD
PrintD:4
发生在:PrintE
PrintE:5
===如果没有接口的话,在循环中必须用以上六个类的变量来分别获取数据元素==========
-----lei--------------
发生在:BClass
发生在:FClass
发生在:BClass
-----virtual--------------
发生在:BClass
发生在:FClass
发生在:F1Class
发生在:FClass
发生在:BClass
请按任意键继续. . .

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:41119次
    • 积分:852
    • 等级:
    • 排名:千里之外
    • 原创:41篇
    • 转载:9篇
    • 译文:0篇
    • 评论:10条
    文章分类
    最新评论