如何在非静态内部类的另一个实例中引用外部类?
2022-09-04 04:21:05
我正在使用Apache Commons EqualsBuilder为非静态Java内部类构建equals方法。例如:
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
}
}
除了声明方法之外,是否有语法可以引用 的引用?类似的东西(这不起作用)?other
Foo
getMyFoo()
other.Foo.this