需要包含X.Y.Z的封闭实例 [英] an enclosing instance that contains X.Y.Z is required

查看:470
本文介绍了需要包含X.Y.Z的封闭实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人能帮助我.我已经研究了这个问题几个小时,似乎没有其他人遇到过同样的问题(我遇到过).我一定是犯了一些非常愚蠢的错误.

I'm hoping someone can help me out. I've been researching this problem for hours and nobody else seems to have had the same issue (that I've come across). I must be making some really bonehead mistake.

我是 Java 的新手,并且设计了一个非常简单的 Java 程序.

I'm new to Java and have designed a very simple Java program.

我有一个抽象的超类,叫做Student.此Student超类具有3个扩展的类,分别称为GraduateUndergraduatePartTime. Student类具有几种抽象方法和几种非抽象方法.我已经验证了Students中定义的所有抽象方法都已在所有3个扩展类中实现.

I have an abstract superclass called Student. This Student superclass has 3 extending classes called Graduate, Undergraduate, and PartTime. The Student class has several abstract methods and several non-abstract methods. I've verified that all abstract methods defined in Students have been implemented in all 3 extended classes.

现在,我正在尝试做一些应该非常简单的事情.我正在尝试实例化这些扩展类之一.如果我这样做:

Now I'm trying to do something that should be extremely simple. I'm trying to instantiate one of these extended classes. If I do this:

Student student = new Student();

Netbeans Student is abstract; can't be instantiated.好的,我知道为什么不能实例化抽象类.然后我尝试一下:

Netbeans says Student is abstract; can't be instantiated. Ok, fine, I understand why abstract classes cannot be instantiated. Then I try this:

Student.Graduate student = new Graduate();

然后 Netbeans an enclosing instance that contains studentmanager.Student.Graduate is required(studentmanager是我的软件包名称).我不知道那是什么意思.但是,我确实发现我可以像这样实例化Student而不会出现错误:

And Netbeans says an enclosing instance that contains studentmanager.Student.Graduate is required (studentmanager is my package name). I can't figure out what that means. However, I did figure out that I can instantiate Student like this without errors:

Student[] student = new Student[1];

但是,如果我然后尝试做下一个合乎逻辑的事情:

However, if I then try to do the next logical thing:

student[0] = new Graduate();

我遇到相同的an enclosing instance...错误.

最重要的是,我想知道如何实例化Graduate.谁能帮我吗?任何见识将不胜感激!

Bottom line is I'd like to know how I can instantiate Graduate. Can anyone help me out? Any insight would be greatly appreciated!

谢谢.

推荐答案

您是否将GraduateUndergraduatePartTime定义为Student内部类?也就是说,像这样:

Did you define Graduate, Undergraduate, and PartTime as inner classes of Student? That is, like this:

public class Student {
    /* ... */
    public class Graduate extends Student { /* ... */ }
    public class Undergraduate extends Student { /* ... */ }
    public class PartTime extends Student { /* ... */ }
}

内部类的

实例具有对 enclosing 类的实例的隐式引用,因此它可以执行访问封闭实例的私有字段和方法的操作.当您尝试实例化其中一个子类的实例时,编译器会说:嘿,按住,我不知道该将隐式引用设置为什么!"并产生错误.

Instances of inner classes have an implicit reference to an instance of the enclosing class, so that it can do things like access the enclosing instance's private fields and methods. When you try to instantiate an instance of one of the subclasses, the compiler says "Hey, hold up, I don't know what to set the implicit reference to!" and produces an error.

有三种可能的解决方案:

There are three possible solutions:

  • 您可以将三个子类从Student类中移出,并在自己的文件中定义它们自己的顶级类.
  • 您可以在子类的声明之前添加关键字static.这告诉编译器,您不需要从内部类到封闭类的隐式引用.这样可以防止您执行诸如从子类访问Student的私有字段和方法之类的事情,但是如果您仍然不需要这样做,那就没有意思了.
  • 如果 do 需要子类来访问Student的私有成员,则可以创建Student的实例,然后使用此特殊语法创建内部的实例.课.

  • You could move the three subclasses out of the Student class into their own top-level classes defined in their own files.
  • You could add the keyword static in front of the declarations of the subclasses. This tells the compiler that you do not want an implicit reference from the inner class to the enclosing class. This will prevent you from doing things like accessing Student's private fields and methods from a subclass, but if you don't need to do that anyway, this is moot.
  • If you do need the subclasses to access private members of Student, you can create an instance of Student and then use this special syntax to create an instance of the inner class.

Student s = new Student();
Graduate g = s.new Graduate();

此示例中的s.new告诉编译器Graduate的新实例应具有对s的隐式引用.

The s.new in this example tells the compiler that the new instance of Graduate should have an implicit reference to s.

请注意,在此示例中,这不太可能是您想要的.具有内部类的实例通常可以很有用,这些实例可以直接修改其封闭实例的字段来执行诸如响应回调而更新封闭类的状态之类的事情,但是我怀疑这不是您的意图.

Note that in this example this is very unlikely to be what you want. It is often useful to have instances of inner classes that can directly modify their enclosing instance's fields for doing things like updating the state of the enclosing class in response to a callback, but I suspect that is not your intention here.

这篇关于需要包含X.Y.Z的封闭实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆