<分区>
显式实现接口(interface)和实现接口(interface)有什么区别。
当您从接口(interface)派生类时,智能感知会建议您同时执行这两项操作。
但是,有什么区别呢?
<分区>
显式实现接口(interface)和实现接口(interface)有什么区别。
当您从接口(interface)派生类时,智能感知会建议您同时执行这两项操作。
但是,有什么区别呢?
最佳答案
另一方面:
如果您隐式实现,这意味着您的类的用户可以访问接口(interface)成员,而无需强制转换。
如果它是显式实现的,则客户必须先将您的类强制转换为接口(interface),然后才能访问成员。 下面是一个显式实现的示例:
interface Animal
{
void EatRoots();
void EatLeaves();
}
interface Animal2
{
void Sleep();
}
class Wombat : Animal, Animal2
{
// Implicit implementation of Animal2
public void Sleep()
{
}
// Explicit implementation of Animal
void Animal.EatRoots()
{
}
void Animal.EatLeaves()
{
}
}
您的客户端代码
Wombat w = new Wombat();
w.Sleep();
w.EatRoots(); // This will cause a compiler error because it's explicitly implemented
((Animal)w).EatRoots(); // This will compile
关于c# - C# 接口(interface)的隐式和显式实现之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/712657/
相关文章:
interface - Ifort 编译器无法识别 Fortran 中的接口(interface)
arrays - 如何使用 typescript 的字符串索引接口(interface)?
c# - 为什么 Entity Framework 有 AddAsync?
c# - 为什么最小化和恢复窗口会对 WPF 中已绘制的图形产生影响?
c# - 如何在用户输入时替换文本框中的字符? (在 C# 中)
c++ - Crypto++ PKCS5_PBKDF2_HMAC 类签名的原因?
javascript - 尝试在 Angular 客户端显示数据