介面實現方式(c )

2021-04-18 06:53:06 字數 1223 閱讀 4834

如果類實現兩個介面,並且這兩個介面包含具有相同簽名的成員,那麼在類中實現該成員將導致兩個介面都使用該成員作為它們的實現。例如:

//介面

using system;

using system.collections.generic;

using system.text;

inte***ce isu***ce

}//介面實現類(隱式實現)

using system;

using system.collections.generic;

using system.text;

#endregion

}}//效果測試類

using system;

using system.collections.generic;

using system.text;

}}

然而,如果兩個介面成員執行不同的函式,那麼這可能會導致其中乙個介面的實現不正確或兩個介面的實現都不正確。可以顯式地實現介面成員 -- 即建立乙個僅通過該介面呼叫並且特定於該介面的類成員。這是使用介面名稱和乙個句點命名該類成員來實現的。例如:

//介面

using system;

using system.collections.generic;

using system.text;

}inte***ce itest1

}//介面實現類(顯示實現)

using system;

using system.collections.generic;

using system.text;

#region itest 成員

bool itest.getresult(string username)

return check;

}public int p

}#endregion

#region itest1 成員

int itest1.p(int a,int b)

bool itest1.getresult(int age)

return check;

}#endregion

}}//測試效果類

using system;

using system.collections.generic;

using system.text;

}}

idea 呼叫c 介面 C 中對於介面的實現方式

c 中對於介面的實現方式有隱式介面和顯式介面兩種 隱式地實現介面成員 建立乙個介面,ichinese,包含乙個成員 speak 我們建立乙個類speaker,實現介面chinese 隱藏式實現例子 public inte ce ichinese string speak public class s...

idea 呼叫c 介面 C 介面的三種實現方式

public inte ce myinte ce 下面三個方法的簽名都是 method public hidebysig newslot abstract virtual public virutal abstract void method1 void method2 void method3 c...

C 介面的三種實現方式

原文c 介面的三種實現方式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 4...