如何解决“需要包含 X.Y 的封闭实例"? [英] How can I resolve "an enclosing instance that contains X.Y is required"?

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

问题描述

我正在 Netbeans 中开发一个小型桌面应用程序.这是我的第一个程序,我面临着一种非常奇怪的错误.我知道我做错了什么,但无法追踪我做错了什么:(

I am developing a small desktop application in Netbeans. This is my first program and I am facing a very strange type of error. I know I did some thing wrong but unable to trace what I am doing wrong :(

请帮助我解决此错误.

说明:我有一个默认包 Src 并且我正在根据需要在这个包中创建新的 Java 类.与其他类一起,我创建了一个 X 类,如下所示:

Description: I have a default package Src and I am creating new Java classes in this package as required. Along with other classes I made a class X like this:

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
}

现在我需要在同一个包中存在的其他类中创建一个内部类的实例,如下所示:

Now I need to create instance of one of the inner classes in some other class that exists in the same package, like this:

public X.Y oY = new X.Y();

但我收到以下错误:

需要一个包含 X.Y 的封闭实例

an enclosing instance that contains X.Y is required

请帮助我解决此错误.

推荐答案

首先要创建一个 X 类(外层类)的对象,然后使用 objX.new InnerClass() 语法创建一个 Y 类的对象.

First of all you have to create an object of X class (outer class) and then use objX.new InnerClass() syntax to create an object of Y class.

试试,

X x=new X();
X.Y y=x.new Y();

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

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