为什么我可以密封实现接口的类,但不能密封成员?
给定这个接口
public interface IMyInterface
{
string Method1();
}
为什么这是有效的
public sealed class InheretedFromInterfaceSealed: IMyInterface
{
public string Method1()
{
return null;
}
}
但这不是
public class InheretedFromInterfaceWithSomeSealed: IMyInterface
{
public sealed string Method1()
{
return null;
}
}
但它对于抽象类来说是一个有效的场景
public abstract class AbstractClass
{
public abstract string Method1();
}
public class InheretedFromAbstractWithSomeSealed: AbstractClass
{
public sealed override string Method1()
{
return null;
}
}
Given this interface
public interface IMyInterface
{
string Method1();
}
Why is this valid
public sealed class InheretedFromInterfaceSealed: IMyInterface
{
public string Method1()
{
return null;
}
}
But this isnt
public class InheretedFromInterfaceWithSomeSealed: IMyInterface
{
public sealed string Method1()
{
return null;
}
}
And yet it is a valid scenario for an abstract class
public abstract class AbstractClass
{
public abstract string Method1();
}
public class InheretedFromAbstractWithSomeSealed: AbstractClass
{
public sealed override string Method1()
{
return null;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为默认情况下每个方法都是密封的,除非它是虚拟的,或者除非您不对已经是虚拟的并且您要覆盖的东西说
密封
。Because every method is by default sealed, unless it's virtual, or unless you don't say
sealed
on something that's already virtual and that you're overriding.默认情况下,类中的每个方法都是密封的(在 VB.NET 中为
NotOverridable
),除非您专门将其声明为virtual
(在 VB.NET 中为Overridable
)。网)。正如您所说,类的情况并非如此。您必须明确指出您想要禁止使用
sealed
(或VB.NET 中的NotInheritable
)从类继承。Every method in a class is sealed (
NotOverridable
in VB.NET) by default, unless you specifically declare it asvirtual
(Overridable
in VB.NET).As you've said, this is not the case with classes. You have to specifically indicate that you want to forbid inheriting from a class using
sealed
(orNotInheritable
in VB.NET).只是提醒一下,C# 中的接口方法不能被
密封
。考虑以下代码:
然后,如果我们有
var d = new Derived()
,我们将有:d.Bar()
写入Derived.Bar
((Base)d).Bar()
写入Base.Bar
((IFoo)d).Bar()
写入Derived.Bar
((IFoo)(Base)d).Bar()
写入Derived.Bar
接口方法
Bar 被派生类覆盖。被
密封
的方法不是接口方法,而是Base
的方法。也就是说,隐式实现
应被视为以下语义上等效的显式实现:
如果
ImplicitImplEquiv
的派生类只是用另一个public 隐藏
,调用public void Bar
void Bar((IFoo)ref).Bar()
仍会调用ImplicitImplEquiv.Bar
。但如果派生类也重新继承IFoo
并提供新的实现,则接口vtable将与ImplicitImplEquiv
不同。Just a reminder that interface methods in C# cannot be
sealed
.Consider the following code:
And then, if we have
var d = new Derived()
, we'll have:d.Bar()
writesDerived.Bar
((Base)d).Bar()
writesBase.Bar
((IFoo)d).Bar()
writesDerived.Bar
((IFoo)(Base)d).Bar()
writesDerived.Bar
The interface method
Bar
is overridden by the derived class. The method that issealed
is not the interface method, but a method ofBase
.That is to say, an implicit implementation
should be considered as the following semantically equivalent explicit implementation:
If a derived class of
ImplicitImplEquiv
simply hidespublic void Bar
with anotherpublic void Bar
, calling((IFoo)ref).Bar()
will still invoke theImplicitImplEquiv.Bar
. But if the derived class also reinheritsIFoo
and provides a new implementation, the interface vtable will be different than that ofImplicitImplEquiv
.