有人请告诉我以下使用sealed不编译的原因吗?然而,如果我更换sealed与final和编译如Java,它的工作原理.
private sealed int compInt = 100;
public bool check(int someInt)
{
if (someInt > compInt)
{
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
Joe*_*oey 59
这是因为final在Java中意味着很多不同的东西取决于你使用它的位置,而sealed在C#中只适用于类和潜在的虚拟成员(方法,属性,事件).
在Java final中可以应用于:
sealed.virtual为派生类,并且可以sealed再次阻止此更多派生类.readonly.Sealedin C#只能应用于引用类型,并且对继承树有影响。
在实践中,标记为sealed保证是继承树中最后一个“叶子” 的类型,或者简而言之,您不能从像a这样声明的类型中派生sealed。
public sealed class Child : Base
{
}
public class AnotherAgain : Child //THIS IS NOT ALLOWED
{
}
Run Code Online (Sandbox Code Playgroud)
它不能应用于成员。