gpt4 book ai didi

java - 如何在非静态内部类的另一个实例中引用外部类?

转载 作者:搜寻专家 更新时间:2023-10-30 19:45:56 26 4
gpt4 key购买 nike

我正在使用 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
}
}

除了声明 getMyFoo() 方法之外,是否有语法可以引用 otherFoo 引用?像 other.Foo.this 这样的东西(不起作用)?

最佳答案

没有。

最好的方法可能就是您所建议的:将 getFoo() 方法添加到您的内部类。

关于java - 如何在非静态内部类的另一个实例中引用外部类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/309737/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com