有C#密封和Java的final关键字之间的功能差异? [英] Is there any functional difference between c# sealed and Java's final keyword?

查看:273
本文介绍了有C#密封和Java的final关键字之间的功能差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:结果
什么是Java的决赛在C#中等价?




$ b $ ; b

在Java的最终应用到的不仅仅是一个类的更多



所以,我在想:是有两个关键字之间的功能差异<? / p>

感谢你了,对不起相对noob问题。



一个快速谷歌搜索没有满足我的需求。


解决方案

Java的最后关键字是C#的<$ C $相当于C>密封,只读密封关键字。



这三个中有两个在Java和C#有所不同:



在Java中,方法是在默认情况下虚拟的,所以任何方法可以声明最后来防止它被覆盖。在C#中,方法不是默认的虚拟,所以只能覆盖方法可以声明密封(以防止它们被进一步覆盖)



在Java中,任何变量或字段可以声明最终来防止它初始化之后被改变(并且,对于字段,构造之外)。在C#中,字段可以声明只读,对于完全相同的效果,但局部变量不能。注意,Java没有类似的C#常量关键字。 (常量 S IN C#在编译时计算,值是硬编码,而不是引用)



对于类,C#的密封类和Java的最后类是完全一样的(据我所知)。


Possible Duplicate:
What is the equivalent of Java’s final in C#?

In Java final applies to more than just a class.

So, I wonder: is there any functional difference between the two keywords?

Thank you, and sorry for a relatively noob question.

A quick Google search did not satisfy my needs.

解决方案

Java's final keyword is the equivalent of C#'s sealed, readonly, and sealed keywords.

Two of those three are somewhat different in Java and C#:

In Java, methods are virtual by default, so any method can be declared final to prevent it from being overriden. In C#, methods are not virtual by default, so the only overridden methods can be declared sealed (To prevent them from being further overridden)

In Java, any variable or field can be declared final to prevent it from being changed after initialization (And, for fields, outside the constructor). In C#, fields can be declared readonly, for the exact same effect, but local variables cannot. Note that Java has no equivalent of C#'s const keyword. (consts in C# are evaluated at compile-time, and the values are hard-coded instead of being referenced)

For classes, C#'s sealed classes and Java's final classes are exactly the same (AFAIK).

这篇关于有C#密封和Java的final关键字之间的功能差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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