c#中的Java内部类 [英] Java inner classes in c#
本文介绍了c#中的Java内部类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下Java代码:
I have the following Java code:
public class A {
private int var_a = 666;
public A() {
B b = new B();
b.method123();
System.out.println(b.var_b);
}
public class B {
private int var_b = 999;
public void method123() {
System.out.println(A.this.var_a);
}
}
}
产生666和999。现在,我已经尝试在c#中设置类似的代码,但似乎无法实现相同的功能。如果是这种情况,你在c#编程时通常会如何达到类似的效果?
Which yields 666 and 999. Now, I've tried to set up similar code in c#, but it seems that it is not possible to accomplish the same. If that's the case, how you usually achieve a similar effect when programming in c#?
推荐答案
你需要让内部课采取外部类的实例作为构造函数参数。 (这是Java编译器实现内部类的方式)
You need to make the inner class take an instance of the outer class as a constructor parameter. (This is how the Java compiler implements inner classes)
这篇关于c#中的Java内部类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文