关于内部/本地/匿名类,Java和C#之间的根本区别是什么? [英] What are the fundamental differences between Java and C# in terms of inner/local/anonymous classes?
问题描述
Java和C#在内部/本地/匿名类方面的根本区别是什么?
What are the fundamental differences between Java and C# in terms of inner/local/anonymous classes?
推荐答案
C#没有Java内部类的等价物 - 它只有嵌套类型(如Java的静态嵌套类)。
C# doesn't have the equivalent of Java inner classes - it only has nested types (like Java's "static" nested classes).
访问规则略有不同 - 在Java中,外部类可以访问其嵌套类的私有成员,反之亦然。在C#中,嵌套类可以访问外部类的私有成员,但不是。
The access rules are slightly different - in Java, an outer class has access to its nested class's private members, and vice versa. In C# the nested class has access to the outer class's private members, but not the other way round.
C#没有匿名内部类像Java,但它有具有匿名方法和lambda表达式,这是一个更清洁的方式实现通常使用的匿名内部类。两个机制的变量捕获不同 - 有关详情,请参见我的关闭文章 。
C# doesn't have anonymous inner classes like Java, but it does have anonymous methods and lambda expressions, which are a much cleaner way of achieving most of what anonymous inner classes are usually used for. The variable capture for the two mechanisms is different - see my article on closures for more details.
这篇关于关于内部/本地/匿名类,Java和C#之间的根本区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!