创建Java内部类的编译错误处理
2013-05-23 09:14
253 查看
请大家尊重劳动成果,转载注明出处:http://blog.csdn.net/caoshichao520326/article/details/8961297
在创建非静态内部类时,经常会遇到“No enclosing instance of type * is accessible. Must qualify the allocation with an enclosing instance of type *(e.g. x.new A() where x is an instance of *).”这样的报错,其实原因只有一点,内部类是依赖于外部类存在的,所以在使用非静态内部类时,要求先实例化外部类才可以使用内部类。关于非静态内部类,我们可以把它理解成外部类的成员变量,我们在使用一个类的非静态成员变量时要求先对类进行实例化,然后通过对象来调用这个类的非静态成员变量。这里非静态内部类同外部类的关系,就如同非静态成员变量同类的关系一样。所以在使用非静态内部类时,要求先实例化外部类。
下面我给出例子来分析一下:
上面的代码的第16行将会报出“No enclosing instance of type OuterClass is accessible. Must qualify the allocation with an enclosing instance of type OuterClass (e.g. x.new
A() where x is an instance of OuterClass).”这样的编译错误。错误的原因如上面红色字体所述。
解决方法一:将非静态内部类转换成静态内部类,即在上面程序的第21行的“Private”后面加上“Static”即可。
解决方法二:先实例化外部类,然后通过外部类来调用内部类的构造函数,代码如下:
上面代码的第16行先进行了外部类的实例化,第18行通过外部类来引用内部类,这样就不会出现“No enclosing instance of type OuterClass is
accessible. Must qualify the allocation with an enclosing instance of type OuterClass (e.g. x.new A() where x is an instance of OuterClass”这个编译报错了。
在创建非静态内部类时,经常会遇到“No enclosing instance of type * is accessible. Must qualify the allocation with an enclosing instance of type *(e.g. x.new A() where x is an instance of *).”这样的报错,其实原因只有一点,内部类是依赖于外部类存在的,所以在使用非静态内部类时,要求先实例化外部类才可以使用内部类。关于非静态内部类,我们可以把它理解成外部类的成员变量,我们在使用一个类的非静态成员变量时要求先对类进行实例化,然后通过对象来调用这个类的非静态成员变量。这里非静态内部类同外部类的关系,就如同非静态成员变量同类的关系一样。所以在使用非静态内部类时,要求先实例化外部类。
下面我给出例子来分析一下:
package com.csc.innerclasstest; /** * * @author csc * */ //外部类 public class OuterClass { /** * @param args */ public static void main(String[] args) { InnerClass innerClass = new InnerClass(); innerClass.say(); System.out.println("I am in OuterClass!"); } //定义一个内部类 private class InnerClass{ private void say() { System.out.println("I am in InnerClass!"); } } }
上面的代码的第16行将会报出“No enclosing instance of type OuterClass is accessible. Must qualify the allocation with an enclosing instance of type OuterClass (e.g. x.new
A() where x is an instance of OuterClass).”这样的编译错误。错误的原因如上面红色字体所述。
解决方法一:将非静态内部类转换成静态内部类,即在上面程序的第21行的“Private”后面加上“Static”即可。
解决方法二:先实例化外部类,然后通过外部类来调用内部类的构造函数,代码如下:
package com.csc.innerclasstest; /** * * @author csc * */ //外部类 public class OuterClass { /** * @param args */ public static void main(String[] args) { //实例化外部类 OuterClass outerClass = new OuterClass(); //通过外部类引用内部类 InnerClass innerClass = outerClass.new InnerClass(); innerClass.say(); System.out.println("I am in OuterClass!"); } //定义一个内部类 private class InnerClass{ private void say() { System.out.println("I am in InnerClass!"); } } }
上面代码的第16行先进行了外部类的实例化,第18行通过外部类来引用内部类,这样就不会出现“No enclosing instance of type OuterClass is
accessible. Must qualify the allocation with an enclosing instance of type OuterClass (e.g. x.new A() where x is an instance of OuterClass”这个编译报错了。
相关文章推荐
- 创建Java内部类的编译错误处理
- 创建Java内部类的编译错误处理
- 创建Java内部类的编译错误处理
- 创建Java内部类的编译错误处理
- 创建项目后NDK编译警告当作错误处理的解决方案
- Windows&Eclipse&Qt2010.02编译错误的处理
- xcode7编译老版本程序时提示错误的处理方法
- Android编译错误处理
- VC 6.0工程转VC 2005工程的编译错误----消息处理宏
- 关于VC2008编译出现错误“error C2664: ” 的处理方法:
- Linux系统平台下关于GCC编译及使用的方法(-Werror,它要求GCC将所有的警告当成错误进行处理 )
- 编译ncurses错误处理及解决办法
- .net C# 动态编译 cs2001 错误 处理
- Mac OS X下编译并搭建ffmpeg开发环境及常见错误处理
- S5PV210 U-BOOT 1.4.3编译错误的处理
- qt编译错误的处理
- 编译内核时出现 警告:检测到时钟错误。您的创建可能是不完整的。
- ubuntu 10 编译内核 2.6.18 错误处理
- 使用spring tool suite(STS)工具创建spring boot项目和出现错误后的处理
- 使用PROC编译.pc文件时错误情况及处理