从内部匿名类访问外部匿名类的字段
要从内部类 B 访问外部类 A 的字段 x,我意识到您可以使用“A.this.x”。 但是如果外部类也是匿名的怎么办? 例如,
public class Main1 {
public static void main(String[] args) {
Comparable c1 = new Comparable(){
int x = 3;
public int compareTo(Object o) {
Comparable c2 = new Comparable(){
int x = 4;
public int compareTo(Object o) {
return x; // <-- THIS LINE
}
};
return c2.compareTo(o);
}
};
System.out.println(c1.compareTo(null));
}
}
当运行此代码时,会打印值 4,因为这是 c2 的字段 x 的值。 但是,我想更改标记为“THIS LINE”的行,以便它返回外部类的 x (即 c1 的字段 x,值为 3)。 如果外部类(即 c1 的类)是命名类 A,那么我可以替换
return x;
为
return A.this.x;
但由于外部类也是匿名的,所以我没有可用的名称。
问题:有没有办法修改标记为“THIS LINE”的行,使其引用 c1 的字段 x 而不是 c2 的字段,而不将匿名类更改为命名类?
我意识到这段代码真的很丑陋,并且以这种方式使用匿名类不是好的编程风格,但代码是由另一个程序生成的,这是实现生成器的最简单方法。
To access the field x of an outer class A from an inner class B, I realize that you can use "A.this.x". But what if the outer class is also anonymous? For example,
public class Main1 {
public static void main(String[] args) {
Comparable c1 = new Comparable(){
int x = 3;
public int compareTo(Object o) {
Comparable c2 = new Comparable(){
int x = 4;
public int compareTo(Object o) {
return x; // <-- THIS LINE
}
};
return c2.compareTo(o);
}
};
System.out.println(c1.compareTo(null));
}
}
When this code is run, the value of 4 is printed because that is the value of c2's field x. However, I would like to change the line marked "THIS LINE" so that it returns the outer class's x (that is, c1's field x, with the value 3). If the outer class (that is, c1's class) were a named class A, then I could replace
return x;
with
return A.this.x;
But since the outer class is also anonymous, I don't have a name to use.
Question: Is there a way to modify the line labeled "THIS LINE" so that it refers to c1's field x rather than c2's, without changing the anonymous classes into named classes?
I realize this code is really ugly and it is not good programming style to use anonymous classes this way, but the code is being generated by another program, and this is the easiest way to implement the generator.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我会通过选择 x 以外的名称来避免隐藏其他变量。
I would avoid hiding the other variable by choosing a name other than x.
简单的答案是不要隐藏变量:
输出:
鉴于您正在使用生成的代码,这适合您吗?
The simple answer is to not shadow the variables:
Output:
Given that you are working with generated code, is this an option for you?
AFAIK 没有办法实现你想要的。 如果您可以更改代码(正如您似乎能够做到的那样)但不想将它们更改为命名类,您可以只更改变量的名称,这样就不会出现范围问题吗? 无论如何,为了清楚起见,你应该重命名它们
AFAIK there's no way to achieve what you want. If you can change the code (as you appear to be able to) but don't want to change them into named classes, can you just change the name of the variables so you don't have scope issues? You should rename them for clarity anyway
类是匿名的——无名的。 无法使用名称引用访问其字段。 没有名字。
Classes are anonymous -- nameless. No way of accessing their fields using name reference. There's no name.
我相信您必须将这些字段声明为final - 您那里的代码仅因您所抱怨的阴影而进行编译。 不对。这看起来像一个经典案例,其中类似 Lisp 的 gensym 会让事情变得更容易。 换句话说,让代码生成器对这些变量使用不同的标识符——例如x1 和x2。
I believe you will have to declare the fields final -- the code you have there compiles only because of the shadowing you are complaining about. Not true.This looks like a classic case where something like Lisp's gensym will make things easier. In other words, have the code generator use different identifiers for those variables -- e.g. x1 and x2.