C# interface定义及使用的问题:接口定义以大写字母I开头。方法只定义其名称,在C#中,方法默认是公有方法;用public修饰方法是不允许的,否则会出现编译错误;接口可以从别的接口继承,如果是继承多个接口,则父接口列表用逗号间隔。

C# interface可以通过类来实现,当类的基列表同时包含基类和接口时,列表中首先出现的是基类;类必须要实现其抽象方法;
C# interface定义及使用实例:
- using System;
 - namespace Dage.Interface
 - {
 - //打印机接口
 - public interface IPrint
 - {
 - string returnPrintName();
 - }
 - }
 - //C# interface应用实现
 - using System;
 - using Dage.Interface;
 - namespace Dage.Print
 - {
 - //HP牌打印机类
 - public class HP: IPrint
 - {
 - public string returnPrintName()
 - {
 - return "这是HP牌打印机";
 - }
 - }
 - }
 - //C# interface应用实现
 - using System;
 - namespace Dage.Print
 - {
 - //Eps牌打印机类
 - public class Eps: IPrint
 - {
 - public string returnPrintName()
 - {
 - return "这是Eps牌打印机";
 - }
 - }
 - }
 - //C# interface应用实现
 - using System;
 - using Dage.Interface;
 - namespace Dage
 - {
 - //打印类
 - public class Printer
 - {
 - public Printer()
 - {}
 - public string PrintName(IPrint iPrint)
 - {
 - return iPrint.returnPrintName();
 - }
 - }
 - }
 - //C# interface应用实现
 - --WinFrom中调用代码:
 - private void button1_Click(object sender, System.EventArgs e)
 - {
 - Printer p= new Printer();
 - switch (this.comboBox1.Text)
 - {
 - case "HP":
 - MessageBox.Show(p.PrintName(new HP()));
 - break;
 - case "Eps":
 - MessageBox.Show(p.PrintName(new Eps()));
 - break;
 - default:
 - MessageBox.Show("没有发现这个品牌!");
 - break;
 - }
 - }
 
C# interface定义与使用的基本内容和相关的理解就向你介绍到这里,希望对你了解和学习C# interface的定义与使用有所帮助。
                新闻标题:C#interface定义及使用浅析
                
                网站链接:http://www.csdahua.cn/qtweb/news2/375602.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网