简体   繁体   English

为什么不能将嵌套类的对象用作超类型构造函数的参数?

[英]Why can't object of nested class be used as a parameter of supertype constructor?

I have code like the following: 我有如下代码:

class A {
  final Object data;

  A(Object _data) {
      data = _data;
  }

  class B extends A {
      B() {
          super(new C());
      }

      class C { }
  }
}

I get following error message: 我收到以下错误消息:

Cannot reference 'C' before supertype constructor has been called

I don't understand why it is not possible. 我不明白为什么这不可能。

Are there any workarounds? 有什么解决方法吗? I want data to be final and classes to be nested as they are in the code above (I don't want to create a different file for each class, because classes are quite small and in my real code it would be more logical for them to be nested) 我希望data是最终的,类要像上面的代码中那样嵌套(我不想为每个类创建一个不同的文件,因为类很小,而在我的实际代码中,它们对它们来说更合逻辑嵌套)

With a simple nested class, it would be fine. 使用简单的嵌套类,就可以了。 However, you're creating an inner class of B , which means you're implicitly passing a reference to an instance of B - which would be this in this case, and you can't use this before super(...) completes. 但是,你要创建一个内部类的B ,这意味着你隐式传递一个参考的实例B -这将是this在这种情况下,你不能使用this之前super(...)完成。

Here's another example - read the comments: 这是另一个示例-阅读评论:

class Base {
   Base(Object x) {}
}

class Outer extends Base {

   Outer() {
       super(new Nested()); // No problem
   }

   Outer(int ignored) {
       super(new Inner()); // Effectively this.new Inner()
   }

   Outer(boolean ignored) {
       super(new Outer().new Inner()); // Fine
   }

   Outer(long ignored) {
       super(new Nested(this)); // Explicitly passing this
   }

   static class Nested {
       Nested() {}

       Nested(Object ignored) {}
   }

   class Inner {
   }
}

The problem is that non-static inner classes need an instance of the outer class before they can be instantiated. 问题在于,非静态内部类在实例化之前需要外部类的实例。 That's why you can even access fields of the outer class directly in the inner class. 这就是为什么您甚至可以直接在内部类中访问外部类的字段的原因。

Make the inner classes static and it should work. 使内部类static ,并且应该可以工作。 In general it's a good pattern to use static inner classes instead of non-static. 通常,使用静态内部类而不是非静态内部类是一个很好的模式。 See Joshua Bloch's Effective Java item 22 for more information. 有关更多信息,请参见Joshua Bloch的Effective Java项目22。

class A {

  final Object data;

  A(final Object _data) {
    data = _data;
  }

  static class B extends A {

    B() {
      super(new C());
    }

    static class C {
    }
  }
}

Hi sorry could not leave a comment. 您好,抱歉无法发表评论。

I don't want to create a different file for each class, because classes are quite small 我不想为每个类创建一个不同的文件,因为类很小

I had a professor that always said "There are never too small classes, only too big ones" 我有一位教授总是说: “永远不会有太小的班级,只有太大的班级”

If you persist and still want the classes to be nested you can make class C static depending on your purpose of course. 如果您坚持并仍然希望嵌套类,则可以根据您的目的将C类静态化。 But I highly recommand to question your design. 但我强烈建议您质疑您的设计。

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

相关问题 不能直接调用超类型构造函数 - 为什么不呢? - Can't call supertype constructor directly - why not? 为什么不将此类视为超类型作为参数? - Why is this class not considered a supertype as a parameter? Java无法覆盖构造函数中使用的方法:Slime类型的setupAnimationFrames()方法必须覆盖或实现超类型方法 - Java can't override method used in constructor: The method setupAnimationFrames() of type Slime must override or implement a supertype method Kotlin对象超类型构造函数 - Kotlin Object Supertype Constructor 为什么不能将GregorianCalendar用作对象类型作为构造函数参数? - Why can't I use GregorianCalendar as an object type as a constructor parameter? 为什么不能在构造函数中实例化该类的相同对象? - Why can't you instantiate the same object of that class inside constructor? 为什么嵌套类不能用作Filter的类 - Why nested class can not be used as class for Filter 如何在单个对象上接受泛型参数的超类型? - How can I accept a supertype of generic parameter on a single object? 为什么我不能将SuperType添加到List <T> 哪里 <T extends SuperType> ? - Why can't I add SuperType to List<T> where <T extends SuperType>? 调用泛型参数的构造函数,或获取Class对象(或为什么它不起作用?) - call constructor, or get Class object, of generic parameter (or why doesn't it work?)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM