简体   繁体   English

在构造函数中初始化需要“ this”作为参数的对象的任何方式?

[英]Any way in initialize objects in a constructor that need “this” as a parameter?

I ran into the problem in Java, but I guess it's a question about OOP in general. 我遇到了Java中的问题,但我想这通常是关于OOP的问题。 It should be a pretty common need, so I hope there's a solution I'm just unaware of. 这应该是非常普遍的需求,所以我希望有一个我不知道的解决方案。

What do you do when you need to initialize an object's fields within the constructor, but those objects need this as a parameter? 当需要在构造函数中初始化对象的字段,但这些对象需要将此作为参数时,该怎么办?

So this is what you can't do: 所以这是你不能做的:

public class SomeClass {
    private SomeOtherClass foo;
    public SomeClass (SomeOtherClass foo) {
         this.foo = foo;
    }
}

public class SomeOtherClass {
     private SomeClass bar;
     public SomeOtherClass() {
          bar = new SomeClass(this);
     }
}

I don't know about any solution except having an init() method that does all object initialization, and calling it after I initialize the SomeOtherClass object in my main program. 我不知道任何解决方案,除了拥有执行所有对象初始化的init()方法,并在主程序中初始化SomeOtherClass对象之后调用它。 Is there a better way? 有没有更好的办法? Or is there a way to make a method of SomeOtherClass (the init() method) run after the constructor is complete, without calling it explicitly? 还是有一种方法可以使SomeOtherClass方法(init()方法)在构造函数完成后运行,而无需显式调用它?

Thanks! 谢谢!

You can do what you've shown. 可以执行显示的操作。 Why do you think it won't work? 您为什么认为它不起作用?

The only limitation is passing this to the superclass constructor (which is a much rarer temptation). 唯一的限制是使this对超类构造函数(这是一个非常罕见诱惑)。 For example, you can't do this: 例如,您不能执行以下操作:

public class SomeSubclass extends SomeSuperclass {
  public SomeSubclass() {
    super(this); /* ERROR: Can't pass `this` to super-ctor. */
  }
}

I've always used initialize() methods for this. 我一直为此使用initialize()方法。 I guess you might be able to spawn some form of thread that runs after object creation, but that's a terrible idea. 我猜您也许能够生成在对象创建后运行的某种形式的线程,但这是一个糟糕的主意。 As far as doing it within the constructor, as you initially asked, I don't see how that would work. 至于您在构造器中进行的操作,正如您最初所问的那样,我不知道它是如何工作的。

To use a car analogy, you're in the middle of building a car (SomeOtherClass), but are unsure of its state of completion. 要使用汽车类比,您正在建造汽车(SomeOtherClass),但是不确定其完成状态。 You want the car to drive on a road (SomeClass), but to use that road you need a complete car. 您希望汽车在道路上行驶(SomeClass),但是要使用该道路,您需要一辆完整的汽车。 So doing what you say would be like passing the road an engine and expecting everything to work out. 因此,按照您说的去做,就像在为引擎加油,并期望一切都能解决。 It just doesn't make sense in OOP terms. 就OOP而言,这没有任何意义。

TL;DR: Use an init() method as you suggested and call it a day. TL; DR:按照您的建议使用init()方法,并称之为一天。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我需要在构造函数中给2个对象作为参数,但是我在努力寻找正确的方法 - I need to give 2 objects as a parameter in a constructor, but I'm struggling to find the right way to do it 如何在父类构造函数所需的子类中初始化参数对象? - How to initialize parameter Objects in child class needed for super class constructor? 用构造函数参数初始化对象数组 - Initialize object array with constructor parameter 构造函数参数是一个对象数组 - Constructor parameter is an array of objects 有没有办法扩展@Configuration 类以包含构造函数参数? - Is there any way to extend the @Configuration class to include the constructor parameter? 在调用超类的构造函数之前,有没有办法在Java中初始化子类的成员变量? - Is there any way to initialize member variables of a subclass in Java before the superclass' constructor is called? 初始化一个类,如果它的构造函数中有一个接口作为参数? - Initialize a Class, if it it has in its constructor an Interface as a Parameter? 我可以在构造函数的参数中初始化数组吗 - can I initialize an array in the parameter of a constructor 如何在构造函数中初始化对象数组? - How to initialize an array of objects within a constructor? 什么是初始化对象的正确方法? - What is the right way to initialize objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM