传递给超级构造函数时,构造函数参数会丢失

时间:2021-03-18 03:49:22

In my GWT app I have a datatype (intended for building and tracking hierarchies of like objects) that extends a superclass, which in turn extends another, abstract superclass. There is a generic parameter declared in the abstract class which is then specified by each subclass as the type of itself. The structure is as follows:

在我的GWT应用程序中,我有一个数据类型(用于构建和跟踪类似对象的层次结构),它扩展了一个超类,后者又扩展了另一个抽象超类。在抽象类中声明了一个泛型参数,然后由每个子类指定为自身的类型。结构如下:

public abstract class AbstractFoo<T extends AbstractFoo> {
     protected T parent;

     protected AbstractFoo(T parent){
          if (parent != null) parent.addChild(this);
          this.parent = parent;
     }
     //...
}

public class Foo<T extends Foo> extends AbstractFoo<T> {
     public Foo(T parent){
          super(parent);
          //...
     }
}

public class SpecialFoo<T extends SpecialFoo> extends Foo<T> {
     public SpecialFoo(T parent){
          super(parent);
          //...
     }
}

When I pass a parent argument to the constructor of SpecialFoo, the constructor of Foo will be called as a superconstructor, and that constructor will in turn call the constructor of AbstractFoo as a superconstructor.

当我将父参数传递给SpecialFoo的构造函数时,Foo的构造函数将被调用为超构造函数,该构造函数将依次调用AbstractFoo的构造函数作为超构造函数。

The problem I have is that the parent argument gets reset to NULL when passed from Foo to AbstractFoo. I have no idea why this happens. Can anyone tell me what I need to do in order to pass it through to the abstract base class unharmed?

我遇到的问题是,当从Foo传递给AbstractFoo时,父参数会重置为NULL。我不知道为什么会这样。任何人都可以告诉我我需要做什么才能将它传递给抽象基类而不受伤害?


EDIT: I think I've solved it... The trick seems to be that I have to declare the parent argument in each subclass, so that there is a more specific reference to it, like so:

编辑:我想我已经解决了......诀窍似乎是我必须在每个子类中声明父参数,以便有更具体的引用,如下所示:

 public abstract class AbstractFoo<T extends AbstractFoo> {
      protected T parent;

      protected AbstractFoo(T parent){
           if (parent != null) parent.addChild(this);
           this.parent = parent;
      }
      //...
 }

 public class Foo<T extends Foo> extends AbstractFoo<T> {
      protected T parent;

      public Foo(T parent){
           super(parent);
           //...
      }
 }

 public class SpecialFoo<T extends SpecialFoo> extends Foo<T> {
      private SpecialFoo parent;

      public SpecialFoo(T parent){
           super(parent);
           //...
      }
 }

2 个解决方案

#1


0  

Have you checked that your parent is non null you give your SpecialFoo constructor?

您是否检查过您的父项是否为空,您是否给出了SpecialFoo构造函数?

#2


0  

Review these points:

回顾这些要点:

  1. Prove POSITIVELY that you are receiving a null argument in the constructor (print it);
  2. 证明你正在构造函数中接收一个null参数(打印它);

  3. Check for other constructors in your classes. You may be calling a constructor you are not expecting to call, especially due to the generic type of that argument.
  4. 检查类中的其他构造函数。您可能正在调用一个您不希望调用的构造函数,尤其是由于该参数的泛型类型。

#1


0  

Have you checked that your parent is non null you give your SpecialFoo constructor?

您是否检查过您的父项是否为空,您是否给出了SpecialFoo构造函数?

#2


0  

Review these points:

回顾这些要点:

  1. Prove POSITIVELY that you are receiving a null argument in the constructor (print it);
  2. 证明你正在构造函数中接收一个null参数(打印它);

  3. Check for other constructors in your classes. You may be calling a constructor you are not expecting to call, especially due to the generic type of that argument.
  4. 检查类中的其他构造函数。您可能正在调用一个您不希望调用的构造函数,尤其是由于该参数的泛型类型。