我正在 Netbeans 中开发一个小型桌面应用程序。这是我的第一个程序,我面临着一种非常奇怪的错误。我知道我做错了什么,但无法追踪我做错了什么:(
请帮我解决这个错误。
说明:
我有一个默认包 Src
并且我正在根据需要在这个包中创建新的 Java 类。与其他类一起,我创建了一个类 X
,如下所示:
public class X
{
public class Y
{//some member functions and variables exist here}
public class Z
{//some member functions and variables exist here}
//some member functions and variables exist here
}
现在我需要在同一个包中存在的其他类中创建内部类之一的实例,如下所示:
public X.Y oY = new X.Y();
但我收到以下错误:
an enclosing instance that contains X.Y is required
请帮我解决这个错误。
最佳答案
首先你要创建一个X类(外部类)的对象,然后使用objX.new InnerClass()
语法创建一个Y类的对象。
试试,
X x=new X();
X.Y y=x.new Y();
关于java - 如何解决 "an enclosing instance that contains X.Y is required"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7619505/