自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#interface定義及使用淺析

開發(fā) 后端
C# interface定義及使用的問(wèn)題是我們?cè)趯?shí)際開發(fā)中會(huì)遇到的,那么有什么具體的解決思路呢?那么本文就向你介紹相關(guān)的內(nèi)容。

C# interface定義及使用的問(wèn)題:接口定義以大寫字母I開頭。方法只定義其名稱,在C#中,方法默認(rèn)是公有方法;用public修飾方法是不允許的,否則會(huì)出現(xiàn)編譯錯(cuò)誤;接口可以從別的接口繼承,如果是繼承多個(gè)接口,則父接口列表用逗號(hào)間隔。

C# interface可以通過(guò)類來(lái)實(shí)現(xiàn),當(dāng)類的基列表同時(shí)包含基類和接口時(shí),列表中首先出現(xiàn)的是基類;類必須要實(shí)現(xiàn)其抽象方法;

C# interface定義及使用實(shí)例:

  1. using System;  
  2. namespace Dage.Interface   
  3. {  
  4.  //打印機(jī)接口  
  5.  public interface IPrint  
  6.  {  
  7.   string returnPrintName();  
  8.  }  
  9. }  
  10. //C# interface應(yīng)用實(shí)現(xiàn)  
  11. using System;  
  12. using Dage.Interface;  
  13. namespace Dage.Print  
  14. {  
  15.  //HP牌打印機(jī)類  
  16.  public class HP: IPrint  
  17.  {  
  18.   public string returnPrintName()  
  19.   {  
  20.    return "這是HP牌打印機(jī)";  
  21.   }  
  22.  }  
  23. }  
  24. //C# interface應(yīng)用實(shí)現(xiàn)  
  25. using System;  
  26. namespace Dage.Print  
  27. {  
  28.  //Eps牌打印機(jī)類  
  29.  public class Eps: IPrint  
  30.  {  
  31.   public string returnPrintName()  
  32.   {  
  33.    return "這是Eps牌打印機(jī)";  
  34.   }  
  35.  }  
  36. }  
  37. //C# interface應(yīng)用實(shí)現(xiàn)  
  38. using System;  
  39. using Dage.Interface;  
  40. namespace Dage  
  41. {  
  42.  //打印類  
  43.  public class Printer  
  44.  {  
  45.   public Printer()  
  46.   {}  
  47.   public string PrintName(IPrint iPrint)  
  48.   {  
  49.    return iPrint.returnPrintName();  
  50.   }  
  51.  }  
  52. }  
  53. //C# interface應(yīng)用實(shí)現(xiàn)  
  54. --WinFrom中調(diào)用代碼:  
  55. private void button1_Click(object sender, System.EventArgs e)  
  56. {  
  57.  Printer p= new Printer();  
  58.  switch (this.comboBox1.Text)  
  59.  {  
  60.   case "HP":  
  61.    MessageBox.Show(p.PrintName(new HP()));  
  62.    break;  
  63.   case "Eps":  
  64.    MessageBox.Show(p.PrintName(new Eps()));  
  65.    break;  
  66.   default:  
  67.    MessageBox.Show("沒有發(fā)現(xiàn)這個(gè)品牌!");  
  68.    break;  
  69.  }  

C# interface定義與使用的基本內(nèi)容和相關(guān)的理解就向你介紹到這里,希望對(duì)你了解和學(xué)習(xí)C# interface的定義與使用有所幫助。

【編輯推薦】

  1. C#語(yǔ)法基礎(chǔ)淺析
  2. 詳解C#break ,continue, return
  3. 淺析C#接口特點(diǎn)及實(shí)例應(yīng)用
  4. C# interface實(shí)例淺析
  5. C# interface編程應(yīng)用淺析
責(zé)任編輯:仲衡 來(lái)源: 博客園
相關(guān)推薦

2009-08-27 13:30:11

C# interfac

2009-08-27 14:12:02

C# interfac

2009-08-31 16:37:20

C#接口定義

2009-08-27 17:11:44

C# Fluent I

2009-07-06 09:23:51

Servlet定義

2009-08-27 17:33:51

Interface接口

2009-08-27 15:17:18

C# interfacinterface使用

2009-08-07 13:39:13

C#定義整型數(shù)組

2009-08-27 17:59:56

C#接口定義

2009-08-12 10:20:52

C#位運(yùn)算符

2009-08-17 13:56:29

C#進(jìn)度條的使用

2009-08-14 15:23:10

C#使用ErrorPr

2010-01-25 10:41:59

C++數(shù)據(jù)類型

2009-09-28 14:45:22

C#接口的定義

2009-08-18 10:30:30

C#枚舉

2009-08-17 17:36:08

C# 枚舉

2009-08-27 15:48:40

interfaceabstract cl

2009-08-19 16:42:41

C#如何使用XML

2009-08-13 13:29:04

C#結(jié)構(gòu)體使用

2009-09-04 15:12:07

C# BinaryWr
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)