在 Java 中使用 extends 会导致封闭实例错误

发布于 2024-09-12 04:45:52 字数 591 浏览 6 评论 0原文

我正在尝试在 Java 中使用扩展(继承)。我创建了一个快速抽象类来扩展,然后扩展它。然而,我的 IDE 现在说“需要一个包含抽象类的封闭实例”,并为派生类的构造函数提供了大错误行。这到底是怎么回事?抽象类没有或不需要任何类型的构造函数。

仅供参考,我部分使用扩展而不是实现,因为我不想为每个相同的派生类维护实现细节,涉及对此使用反射。

编辑:我已经阅读了一些回复。天哪,静态(或非静态)类是什么?只是为了激怒你们所有人,这并没有解决问题。

// some_class.java
public class some_class {
    public static abstract class abstract_class {
        ...
    }
    ...
}

// Model.java
public class Model extends some_class.abstract_class {
    public Model(...) {
        // No enclosing instance! Critical error.
        ...
    }
    ...
}

而且我认为 C++ 的头文件很糟糕。

I'm trying to use extends (inheritance) in Java. I made a quick abstract class to extend from, and then extended it. However my IDE now is saying that "An enclosing instance that contains abstract_class is required" and gives my constructor for the derived classes big error lines. What on earth is it going on about? The abstract class doesn't have or need any sort of constructor.

Just for reference, I'm using extends rather than implements in part because the implementation details that I don't want to have to maintain for every derived class which are identical involve using reflection on this.

Edit: I've read some of the responses. What in God's name is a static (or non-static, for that matter) class? And just to irritate all of you, it didn't solve the problem.

// some_class.java
public class some_class {
    public static abstract class abstract_class {
        ...
    }
    ...
}

// Model.java
public class Model extends some_class.abstract_class {
    public Model(...) {
        // No enclosing instance! Critical error.
        ...
    }
    ...
}

And I thought that C++'s header files were bad.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无人接听 2024-09-19 04:45:52

您发布的代码似乎对我来说编译得很好。尝试在 IDE 中进行干净的构建,它应该可以工作。

只是出于您自己的好奇心,Java 有两种类型的内部类:静态 和常规或(非静态)。如果内部类定义不包含 static 关键字,则意味着该类的实例将始终需要父类的实例。例如:

public class MyClassOuter {
    //...
    public class MyClassInner {
        //..
    }
}

如果您这样写,则可以理解 MyClassInner 的任何实例都将隐式引用 MyClassOuter.

Static,另一方面,hand 并不意味着这样的事情。它只是一个恰好位于另一个类定义内部的类定义。外部类的使用几乎就像一个包(尽管不完全一样)。

The code you posted seems to compile just fine for me. Try doing a clean build in your IDE and it should work.

Just for your own curiosity, Java has 2 types of inner classes: static and regular or (non-static). If you don't include the static keyword for an inner class definition, it means that an instance of that class will always require an instance of the parent class. For ex:

public class MyClassOuter {
    //...
    public class MyClassInner {
        //..
    }
}

If you write that, it is understood that any instance of MyClassInner will have an implicit reference to an instance of MyClassOuter.

Static, on the other, hand implies no such thing. It is just a class definition that happens to be inside another class definition. The outer class is used almost like a package (though not quite).

梦回旧景 2024-09-19 04:45:52

如果你有

interface MyInterface
{

   abstract class MyAbstractClass {
     // ...
   }
}

然后你尝试

class ConcreteClass extends MyAbstractClass {

}

你会得到描述的错误。解决方法是将 MyAbstractClass 移动到顶级类(将其放入自己的文件中 - 对于非公共类来说不是绝对必要的,但可以保持代码的组织性。)或者,添加 static 修饰符到 MyAbstractClass 声明。

if you have

interface MyInterface
{

   abstract class MyAbstractClass {
     // ...
   }
}

and then you try

class ConcreteClass extends MyAbstractClass {

}

You will get the error described. The fix is to either move MyAbstractClass to a top-level class (put it in it's own file - not strictly necessary for non-public classes, but keeps the code organized.) Alternatively, add the static modifier to the MyAbstractClass declaration.

深巷少女 2024-09-19 04:45:52

“封闭实例”消息几乎肯定意味着您的超类有一个(非静态)内部类。在大多数情况下,内部类可以而且应该是静态的——这可能是最好的解决方法。或者,正如消息所述,如果您的父级确实作为非静态内部类有意义,您将需要使用“外部”类的封闭实例。

发布一些代码将有助于消除这些原因之间的歧义,并提出解决问题的最佳方法。我还可以给出具有正确类名称的解决方案的示例 - 目前我认为任意名称不会有那么大的帮助,因为听起来您还没有确定内部/外部类问题。

The "enclosing instance" message almost certainly implies that you have a (non-static) inner class for your superclass. In most cases, inner classes can and should be static - that's likely the best workaround here. Alternatively, as the message says, you will need to use an enclosing instance of the "outer" class, if your parent really makes sense as a non-static inner class.

Posting some code will help disambiguate between these causes and suggest the best way to resolve it. I'll also be able to give examples of the resolutions with the right class names - currently I don't think arbitrary names will help that much as it sounds like you hadn't identified the inner/outer class issue.

初懵 2024-09-19 04:45:52

您需要在子类中添加构造函数 super() 来创建超类。

You need to in your child class add in the constructor super() that super class can be created.

待天淡蓝洁白时 2024-09-19 04:45:52

class A{

 .
 .
 .


 class B{
      . . .     
 }

}

如果你想访问 Class B 并且它不是静态内部类,你可以将代码编写为

A.B objOfB = new A()。新B();

class A{

 .
 .
 .


 class B{
      . . .     
 }

}

if you want to access the Class B and it it is not static inner class you can write the code as

A.B objOfB = new A(). new B();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文