本文介绍了保持对传递到超级构造函数中的新对象的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个好的方法来维护一个对象在一个构造函数中创建的引用,需要传递给一些扩展类的超级构造函数(除了使它可以从超类访问或者传递给构造函数)参数)?



让我以一个例子澄清。使用这个类(我不能修改,并且不能访问foo):

  class TestA {
TestA(Object foo){}
}

扩展此类如下:

  class TestB extends TestA {
Object myCopyOfFoo;

TestB(){
super(new Object());
}
}

有一个很好的方法来存储创建的 new Object() in myCopyOfFoo



三个想法工作:

  TestB(){
myCopyOfFoo = new Object
super(myCopyOfFoo);
}

(错误:构造函数调用必须是构造函数中的第一个语句) p>

  TestB(){
super(myCopyOfFoo = new Object());
}

(错误:无法在显式调用构造函数时引用实例字段myCopyOfFoo)

  TestB(){
super(makeFoo());
}

对象makeFoo(){
myCopyOfFoo = new Object();
return myCopyOfFoo;
}

(错误:无法在显式调用构造函数时引用实例方法) / p>

我想我可以做下面的,但它既不是线程安全也不优雅:

 code> static Object tempFoo; 

TestB(){
super(tempFoo = new Object());
myCopyOfFoo = tempFoo;
}

有没有人有更好的主意?

解决方案

如何:

  class TestB extends TestA {
Object myCopyOfFoo;

//我假设你真的想要这个参数foo?
//我已经离开它的问题...
TestB(){
this(new Object());
}

private TestB(Object copy){
super(copy);
myCopyOfFoo = copy;
}
}

由于第二个构造函数是私有的,在同一个类(或一个封闭类)中调用,所以你只需要确保调用第二个构造函数的任何东西都做了第一个构造函数所做的适当的拷贝。



编辑:如果您的实际情况是您正在获取现有参数的副本,那么这将工作...

  class ClassB extends ClassA {
private final Foo copyOfInput;

ClassB(Foo input){
super(input = input.clone());
copyOfInput = input;
}
}

这很丑陋:(


Is there a good way to maintain a reference to an object created in a constructor that needs to be passed to the super constructor of some extended class (other than making it accessible from the super class or passing it into the constructor as a parameter)?

Let me clarify with an example. Take this class (which I can't modify and which doesn't give me access to foo):

class TestA {
    TestA(Object foo) {}
}

Now, I would like to extends this class as follows:

class TestB extends TestA {
    Object myCopyOfFoo;

    TestB() {
        super(new Object());
    }
}

Is there a good way to store the created new Object() in myCopyOfFoo?

Neither one of these three ideas work:

TestB() {
    myCopyOfFoo = new Object();
    super(myCopyOfFoo);
}

(Error: Constructor call must be the first statement in a constructor)

TestB() {
    super(myCopyOfFoo = new Object());
}

(Error: Cannot refer to an instance field myCopyOfFoo while explicitly invoking a constructor)

TestB() {
    super(makeFoo());
}

Object makeFoo() {
    myCopyOfFoo = new Object();
    return myCopyOfFoo;
}

(Error: Cannot refer to an instance method while explicitly invoking a constructor)

I guess I could do the following, but it's neither thread-safe nor elegant:

static Object tempFoo;

TestB() {
    super(tempFoo = new Object());
    myCopyOfFoo = tempFoo;
}

Does anyone have a better idea for me? And why on earth are my first two ideas not legit?

解决方案

How about:

class TestB extends TestA {
    Object myCopyOfFoo;

    // I assume you actually wanted this to take a parameter called foo?
    // I've left it as per the question...
    TestB() {
        this(new Object());
    }

    private TestB(Object copy) {
        super(copy);
        myCopyOfFoo = copy;
    }
}

As the second constructor is private, it can only be called within the same class (or an enclosing class) so you just need to make sure that anything calling the second constructor has made an appropriate copy, as the first constructor does.

EDIT: If your real situation is that you're taking a copy of an existing parameter, then this would work...

class ClassB extends ClassA {
    private final Foo copyOfInput;

    ClassB(Foo input) {
        super(input = input.clone());
        copyOfInput = input;
    }
}

It's pretty ugly though :(

这篇关于保持对传递到超级构造函数中的新对象的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 08:38