如果dostuff是常规方法,则需要将Bar传递给实例。
class Owner { Bar b = new Bar(this); dostuff(){...}}class Bar { Bar(Owner owner) { owner.dostuff(); }}请注意,Bar可能有很多所有者,但没有任何现实的方法来确定他们是谁。
编辑:您可能正在寻找内部类:示例和注释。
class Owner { InnerBar b = new InnerBar(); void dostuff(){...} void doStuffToInnerBar(){ b.doInnerBarStuf(); } // InnerBar is like a member in Owner. class InnerBar { // not containing a method dostuff. InnerBar() { // The creating owner object is very much like a // an owner, or a wrapper around this object. } void doInnerBarStuff(){ dostuff(); // method in Owner } }}


