C#密封vs Java决赛 [英] C# sealed vs Java final

查看:96
本文介绍了C#密封vs Java决赛的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人请告诉我以下使用密封的原因是不能编译的?然而,如果我用 final 替换 sealed 并将其编译为Java,它就可以工作。

Would anybody please tell me as the reason the following use of sealed does not compile? Whereas, if I replace sealed with final and compile it as Java, it works.

private sealed int compInt = 100;
public bool check(int someInt)
{
    if (someInt > compInt)
    {
        return true;
    }
    return false;
}


推荐答案

那是因为<$ c $ Java中的c> final 意味着很多不同的东西,具体取决于你在哪里使用它而C#中的密封仅适用于 类和潜在的虚拟成员(方法,属性,事件)。

That's because final in Java means plenty of different things depending on where you use it whereas sealed in C# applies only to classes and potentially virtual members (methods, properties, events).

在Java final 可以应用于:

In Java final can be applied to:


  • ,这意味着该类无法继承。这相当于C#的密封

  • 方法,这意味着无法覆盖该方法派生类。这是C#中的默认值,除非您将方法声明为 virtual ,并且在派生类中,对于密封的其他派生类,可以防止这种情况再次。

  • 字段变量,这意味着它们只能初始化一次。对于字段,C#中的等价物是 readonly

  • classes, which means that the class cannot be inherited. This is the equivalent of C#'s sealed.
  • methods, which means that the method cannot be overridden in a derived class. This is the default in C#, unless you declare a method as virtual and in a derived class this can be prevented for further derived classes with sealed again.
  • fields and variables, which means that they can only be initialized once. For fields the equivalent in C# is readonly.

这篇关于C#密封vs Java决赛的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆