如何在非静态内部类的另一个实例中引用外部类? [英] How to refer to the outer class in another instance of a non-static inner class?
本文介绍了如何在非静态内部类的另一个实例中引用外部类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Apache Commons EqualsBuilder为非静态Java内部类构建equals方法。例如:
I'm using the Apache Commons EqualsBuilder to build the equals method for a non-static Java inner class. For example:
import org.apache.commons.lang.builder.EqualsBuilder;
public class Foo {
public class Bar {
private Bar() {}
public Foo getMyFoo() {
return Foo.this
}
private int myInt = 0;
public boolean equals(Object o) {
if (o == null || o.getClass() != getClass) return false;
Bar other = (Bar) o;
return new EqualsBuilder()
.append(getMyFoo(), other.getMyFoo())
.append(myInt, other.myInt)
.isEquals();
}
}
public Bar createBar(...) {
//sensible implementation
}
public Bar createOtherBar(...) {
//another implementation
}
public boolean equals(Object o) {
//sensible equals implementation
}
}
是否有语法可以参考其他
的 Foo
除了声明 getMyFoo()
方法之外?像 other.Foo.this
(这不起作用)?
Is there syntax by which I can refer to other
's Foo
reference apart from declaring the getMyFoo()
method? Something like other.Foo.this
(which doesn't work)?
推荐答案
没有。
最好的方法可能就是你的建议:在你的内部类中添加一个getFoo()方法。
The best way is probably what you suggested: add a getFoo() method to your inner class.
这篇关于如何在非静态内部类的另一个实例中引用外部类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文