简体   繁体   English

传递给超级构造函数的构造函数参数丢失

[英]Constructor argument gets lost when passed to super constructor

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. 在我的GWT应用程序中,我有一个数据类型(用于构建和跟踪类似对象的层次结构),该数据类型扩展了一个超类,而该类又又扩展了另一个抽象超类。 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: 结构如下:

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. 我的问题是,当从Foo传递到AbstractFoo时,parent参数将重置为NULL。 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? 谁能告诉我将其传递给不受损害的抽象基类需要做什么?


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);
           //...
      }
 }

Review these points: 查看以下几点:

  1. Prove POSITIVELY that you are receiving a null argument in the constructor (print it); 积极地证明您在构造函数中接收到null参数(打印出来);
  2. 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. 您可能正在调用一个您不希望调用的构造函数,特别是由于该参数的通用类型。

您是否检查过您的父母是否为非 null给了SpecialFoo构造函数?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM