C#接口的顯隱實現

顯示接口實現與隱式接口實現

何為顯式接口實現、隱式接口實現?簡單概括,使用接口名作為方法名的前綴,這稱為“顯式接口實現”;傳統的實現方式,稱為“隱式接口實現”。下面給個例子。

IChineseGreeting接口,如果需要具備中文的問好行為,需繼承此接口。

?

 1 //IChineseGreeting.cs2 ?3     interface IChineseGreeting4 5     {6 7         void SayHello();8 9     }
10 
11 
12 
13 ChinesePeople類實現IChineseGreeting接口。
14 
15 
16 
17 ?//ChinesePeople.cs
18 ?
19     class ChinesePeople:IChineseGreeting
20 
21     {
22 
23         public void SayHello()
24 
25         {
26 
27             Console.WriteLine("你好!");
28 
29         }
30 
31 
32 
33     }
復制代碼

這樣的實現就是“隱式接口實現”,用public關鍵字作方法的訪問修飾符,我們通常都是這么做的。

你可以這樣調用:

?

  ChinesePeople chinese = new ChinesePeople();chinese.SayHello();   
復制代碼

?

?

也可以這樣調用

?

IChineseGreeting otherChinese = new ChinesePeople();otherChinese.SayHello();
復制代碼

類和接口都能訪問到SayHello()方法。

如果“顯式接口實現”,就應該是這樣。

?

1 //ChinesePeople.cs 2 ? 3 class ChinesePeople:IChineseGreeting 4 5 { 6 7 void IChineseGreeting.SayHello() //注意:接口名限定方法名,并且沒有public訪問修飾符 8 ? 9 { 10 11 Console.WriteLine("你好!"); 12 13 }
}
復制代碼

特別提醒:用于實現一個接口的所有方法都必須具有public可訪問性。但是,假如使用顯式接口實現,不應該為方法添加一個訪問修飾符。

?

這時你只能這樣調用:

?

?

  IChineseGreeting otherChinese = new ChinesePeople();otherChinese.SayHello();
復制代碼

不能通過類來調用

?

ChinesePeople chinese = new ChinesePeople();chinese.SayHello(); //錯誤,不能訪問。
復制代碼

記住:顯示實現,只有接口可以訪問。

?

顯示接口實現看起來有點繁瑣,而且還不能通過類來訪問接口方法。為什么還要有“顯式實現接口”呢?

其實,多個接口可能包含具有相同名稱、相同返回類型和相同參數的方法。如果一個類實現了多個接口,而且接口中的方法具有相同的簽名,就可以通過顯式接口實現來消除它們之間的歧義。顯式接口實現標識出哪個方法屬于哪個接口。

?

IEnglishGreeting接口,如果需要具備英文的問好行為,需繼承此接口。

?

1 //IEnglishGreeting.cs
2 ?
3     interface IEnglishGreeting
4 
5     {  
6 
7         void SayHello();
8 
9     }
復制代碼

可以發現,該接口中的SayHello方法簽名和IChineseGreeting接口中一樣。

?

 1 class ChinesePeople:IChineseGreeting,IEnglishGreeting2 3     {4 5         void IChineseGreeting.SayHello()6 7         {8 9             Console.WriteLine("你好!");
10 
11         }
12 
13         void IEnglishGreeting.SayHello()
14 
15         {
16 
17             Console.WriteLine("Hello!");
18 
19         }
20 
21 
22 
23 }
復制代碼

?

?

可以這樣調用:

?

ChinesePeople chinese = new ChinesePeople();(chinese as IChineseGreeting).SayHello();(chinese as IEnglishGreeting).SayHello();
復制代碼

----------------------------------------------------------------------------------------

此外,如果顯示實現接口的類中還有該類特有的方法,此時會帶來一些不便。

例如,使ChinesePeople具有Play()方法。

?

?

這就帶來一個問題,要調用SayHello()方法時,必須通過接口實現,而此時Play()方法就無法訪問。因為現在將對象看作IChineseGreeting接口了,而接口中沒有Play()方法。

同樣,如果通過類實現,就無法訪問到SayHello()方法。

必須通過強制轉換來解決這個問題。

?

?

            IChineseGreeting chinese = new ChinesePeople();chinese.SayHello();//chinese.Play();  錯誤,無法訪問(chinese as ChinesePeople).Play();//強制轉換后,可以訪問
復制代碼

?

隱式接口實現不存在此問題。

?

總結:

  1. 當類實現一個接口時,通常使用隱式接口實現,這樣可以方便的訪問接口方法和類自身具有的方法和屬性。
  2. 當類實現多個接口時,并且接口中包含相同的方法簽名,此時使用顯式接口實現。即使沒有相同的方法簽名,仍推薦使用顯式接口,因為可以標識出哪個方法屬于哪個接口。
  3. 隱式接口實現,類和接口都可訪問接口中方法。顯式接口實現,只能通過接口訪問。

轉載于:https://www.cnblogs.com/xietianjiao/p/7009415.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/392445.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392445.shtml
英文地址,請注明出處:http://en.pswp.cn/news/392445.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

亞馬遜 各國站點 鏈接_使用Amazon S3和HTTPS的簡單站點托管

亞馬遜 各國站點 鏈接by Georgia Nola喬治亞諾拉(Georgia Nola) 使用Amazon S3和HTTPS的簡單站點托管 (Simple site hosting with Amazon S3 and HTTPS) Hiya folks!大家好! In this tutorial I’ll show you how to host a static website with HTTPS on AWS wit…

leetcode 387. 字符串中的第一個唯一字符(hash)

給定一個字符串,找到它的第一個不重復的字符,并返回它的索引。如果不存在,則返回 -1。 示例: s “leetcode” 返回 0 s “loveleetcode” 返回 2 class Solution { public int firstUniqChar(String s) { int[][] tempnew i…

marlin 三角洲_三角洲湖泊和數據湖泊-入門

marlin 三角洲Data lakes are becoming adopted in more and more companies seeking for efficient storage of their assets. The theory behind it is quite simple, in contrast to the industry standard data warehouse. To conclude this this post explains the logica…

tomcat中設置Java 客戶端程序的http(https)訪問代理

1、假定http/https代理服務器為 127.0.0.1 端口為8118 2、在tomcat/bin/catalina.sh腳本文件中設置JAVA_OPTS,如下圖: 保存后重啟tomcat就能生效。轉載于:https://www.cnblogs.com/zhangmingcheng/p/11211776.html

java界面中顯示圖片_java中怎樣在界面中顯示圖片?

方法一:JLabel helloLabel new JLabel("New label");helloLabel.setIcon(new ImageIcon("E:\\javaSE\u4EE3\u7801\\TimeManager\\asset\\hello.gif"));helloLabel.setBackground(Color.BLACK);helloLabel.setBounds(0, 0, 105, 50);contentPan…

one-of-k 編碼算法_我們如何教K-12學生如何編碼

one-of-k 編碼算法by Christopher George克里斯托弗喬治(Christopher George) 我們如何教K-12學生如何編碼 (How we’re teaching K-12 students how to code) Hello World! (Sorry, I couldn’t resist.) My name is Christopher George and I am currently a Junior at Carn…

knime簡介_KNIME簡介

knime簡介Data Science is abounding. It considers different realms of the data world including its preparation, cleaning, modeling, and whatnot. To be precise, it is massive in terms of the span it covers and the opportunities it offers. Needless to say, th…

hadoop2.x HDFS快照介紹

說明:由于近期正好在研究hadoop的快照機制。看官網上的文檔講的非常仔細。就順手翻譯了。也沒有去深究一些名詞的標準譯法,所以可能有些翻譯和使用方法不是非常正確,莫要介意~~ 原文地址:(Apache hadoop的官方文檔&…

MQTT服務器搭建--Mosquitto用戶名密碼配置

前言: 基于Mosquitto服務器已經搭建成功,大部分都是采用默認的是允許匿名用戶登錄模式,正式上線的系統需要進行用戶認證。 1.用戶參數說明 Mosquitto服務器的配置文件為/etc/mosquitto/mosquitto.conf,關于用戶認證的方式和讀取的…

java number string_java基礎系列(一):Number,Character和String類及操作

這篇文章總結了Java中最基礎的類以及常用的方法,主要有:Number,Character,String。1、Number類在實際開發的過程中,常常會用到需要使用對象而不是內置的數據類型的情形。所以,java語言為每個內置數據類型都…

誰參加了JavaScript 2018狀況調查?

by Sacha Greif由Sacha Greif 誰參加了JavaScript 2018狀況調查? (Who Took the State of JavaScript 2018 Survey?) 我們如何努力使調查更具代表性 (How we’re working to make the survey more representative) I was recently listening to a podcast episode…

機器學習 建立模型_建立生產的機器學習系統

機器學習 建立模型When businesses plan to start incorporating machine learning to enhance their solutions, they more often than not think that it is mostly about algorithms and analytics. Most of the blogs/training on the matter also only talk about taking …

CDH使用秘籍(一):Cloudera Manager和Managed Service的數據庫

背景從業務發展需求,大數據平臺須要使用spark作為機器學習、數據挖掘、實時計算等工作,所以決定使用Cloudera Manager5.2.0版本號和CDH5。曾經搭建過Cloudera Manager4.8.2和CDH4,在搭建Cloudera Manager5.2.0版本號的時候,發現對…

leetcode 455. 分發餅干(貪心算法)

假設你是一位很棒的家長,想要給你的孩子們一些小餅干。但是,每個孩子最多只能給一塊餅干。 對每個孩子 i,都有一個胃口值 g[i],這是能讓孩子們滿足胃口的餅干的最小尺寸;并且每塊餅干 j,都有一個尺寸 s[j]…

壓縮/批量壓縮/合并js文件

寫在前面 如果文件少的話,直接去網站轉化一下就行。 http://tool.oschina.net/jscompress?type3 1.壓縮單個js文件 cnpm install uglify-js -g 安裝 1>壓縮單個js文件打開cmd,目錄引到當前文件夾,cduglifyjs inet.js -o inet-min.js 或者 uglifyjs i…

angular依賴注入_Angular依賴注入簡介

angular依賴注入by Neeraj Dana由Neeraj Dana In this article, we will see how the dependency injection of Angular works internally. Suppose we have a component named appcomponent which has a basic and simple structure as follows:在本文中,我們將看…

leetcode 85. 最大矩形(dp)

給定一個僅包含 0 和 1 、大小為 rows x cols 的二維二進制矩陣,找出只包含 1 的最大矩形,并返回其面積。 示例 1: 輸入:matrix [[“1”,“0”,“1”,“0”,“0”],[“1”,“0”,“1”,“1”,“1”],[“1”,“1”,“1”,“1”,“…

如何查看系統版本

1. winR,輸入cmd,確定,打開命令窗口,輸入msinfo32,注意要在英文狀態下輸入,回車。然后在彈出的窗口中就可以看到系統的具體版本號了。 2.winR,輸入cmd,確定,打開命令窗口,輸入ver&am…

java activemq jmx_通過JMX 獲取Activemq 隊列信息

首先在 activemq.xml 中新增以下屬性在broker 節點新增屬性 useJmx"true"在managementContext 節點配置斷開與訪問服務iP配置成功后啟動下面來看測試代碼/*** Title: ActivemqTest.java* Package activemq* Description: TODO(用一句話描述該文件做什么)* author LYL…

風能matlab仿真_發現潛力:使用計算機視覺對可再生風能發電場的主要區域進行分類(第1部分)

風能matlab仿真Github Repo: https://github.com/codeamt/WindFarmSpotterGithub回購: https : //github.com/codeamt/WindFarmSpotter This is a series:這是一個系列: Part 1: A Brief Introduction on Leveraging Edge Devices and Embedded AI to …