天使半只翼

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C# interface定义及使用的问题: 接口定义以大写字母I开头。

以下未明白:
方法只定义其名称,在C#中,方法默认是公有方法;
用public修饰方法是不允许的,否则会出现编译错误;
接口可以从别的接口继承,如果是继承多个接口,则父接口列表用逗号间隔。

Dage.Interface.cs

using System;
namespace Dage.Interface 
{   //打印机接口
    public interface IPrint  
    {
        string returnPrintName();
    }
}  

Dage.Print.cs

using System;
using Dage.Interface; 
namespace Dage.Print
{  
    //HP牌打印机类  
    public class HP: IPrint   
    { 
        public string returnPrintName()    
        {     return "这是HP牌打印机";    }
    }
    //Eps牌打印机类 
    public class Eps: IPrint   
    {   
        public string returnPrintName()  
        {     return "这是Eps牌打印机";    } 
    }  
}  

Dage.cs

using System;
using Dage.Interface;
namespace Dage  
{   
    //打印类   
    public class Printer 
    {   public Printer(){} 
        public string PrintName(IPrint iPrint) 
        {     
            return iPrint.returnPrintName();    
        }  
    }
}  

 

 

 

 

posted on 2013-01-07 17:26  天使半只翼  阅读(2437)  评论(0编辑  收藏  举报