C#接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/*
 * 1、接口表示一组函数成员而不实现成员的引用类型
 * 2、只有类和结构可以实现接口,必须实现接口中的全部成员
 * 3、接口可以继承接口
 * 4、将类强制转换为类引用为实现的接口的引用时,会抛出异常;用 as 运算符来避免,转换不成功则值为null
 *      ILiveBirth lb =a as ILiveBirth
 *      if(lb!=null)
 * 5、类可以实现任意数量的接口,这些接口成员有重复时,类可以实现单个成员来满足所有包含重复成员的接口。
 * 也可以创建显式接口实现,在实现成员时,限定接口名
 * 
 * 
 */
namespace ExInterFace
{
    interface IIfc
    {
        void PrintOut(string s);
    }
    class MyClass : IIfc 
    {
        public void PrintOut(string s)
        {
            Console.WriteLine("Calling through {0}",s);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            MyClass mc = new MyClass();
            mc.PrintOut("Object");
            IIfc ifc1 = (IIfc)mc;
            ifc1.PrintOut("Interface");
            Console.ReadLine();
        }
    }
}
posted @ 2015-09-10 17:17  Alfred.Xu  阅读(203)  评论(0编辑  收藏  举报