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

查看:80
本文介绍了如何解析“包含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();

但是我收到以下错误:


包含XY的封闭实例是re quired

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天全站免登陆