深夜,临睡前写了个小程序,出了点小问题

public class Test_drive {

    public static void main(String[] args){
A a = new A();              //报错
B b = new B();              //报错
System.out.println(b instanceof A);
}
class A{
int a;
}
class B extends A{
}
}

上面两个语句报错信息如下:

No enclosing instance of type Test_drive is accessible. Must qualify the allocation with an enclosing instance of type Test_drive (e.g. x.new A() where x is an instance of Test_drive).

(1)在stackoverflow上面查找到了类似的问题:http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible/9560633#9560633

(2)下面简单说一下我的理解:

  在这里,A和B都是Test_drive的内部类,类似于普通的实例变量,如果类的静态方法不可以直接调用类的实例变量。在这里,内部类不是静态的内部类,所以,直接赋值(即实例化内部类),所以程序报错。

(3)解决的方法可以有以下两种:

  (1)将内部类定义为static,即为静态类

  (2)将A a = new A();B b = new B();改为:

Test_drive td = new Test_drive();
A a = td.new A();
B b = td.new B();

附注:写到这里好困。如果大家有更好的理解,请在下面留言。谢谢。

再学Java 之 解决No enclosing instance of type * is accessible的更多相关文章

  1. 解决No enclosing instance of type * is accessible

    写一个内部类,并在构造函数中初始化时,遇到报错,搜索问题后发现,有网友出现过类似的问题,下面这个是说的浅显明白的,并确实解决了问题.于是,以下内容照搬过来,不再多费键盘了. public class ...

  2. No enclosing instance of type is accessible. Must qualify the allocation with an enclosing instance of type LeadRestControllerTest (e.g. x.new A() where x is an instance of ).

    java - No enclosing instance is accessible. Must qualify the allocation with an enclosing instance o ...

  3. java中"no enclosing instance of type * is accessible"的解决方法

    这种情况一般发生在“在静态方法里面使用内部类” 测试代码: public class Test { public static void main(String[] args) { A a = new ...

  4. Java中报错No enclosing instance of type caiquan is accessible. Must qualify the allocation with an enclosing instance of type caiquan (e.g. x.new A() where x is an instance of caiquan).

    package test;import java.util.Scanner;import java.util.Random;public class caiquan { public static v ...

  5. 再学Java 之 interface的成员变量

    前言:最近在学多线程,写“哲学家就餐问题(Dining Philosophers)”的时候,需要定义一个全局的变量,即哲学家的人数.常用的做法是在其中一个类中定义一个static final的变量,然 ...

  6. Java中的static(1)【持续更新】——关于Eclipse的No enclosing instance of type ... 错误的理解和改正

    No enclosing instance of type SomeClass is accessible. Must qualify the allocation with an enclosing ...

  7. 【转】Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing

    最近在看Java,在编译写书上一个例子时,由于书上的代码只有一部分,于是就自己补了一个内部类.结果编译时出现:No enclosing instance of type E is accessible ...

  8. Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing

    Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing   ...

  9. Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing--转

    原文:http://blog.csdn.net/sunny2038/article/details/6926079 最近在看Java,在编译写书上一个例子时,由于书上的代码只有一部分,于是就自己补了一 ...

随机推荐

  1. Mysql & Hive 导入导出数据

    ---王燕行转列sql select split(concat_ws(',',collect_set(cast(smzq as string))),',')[1] ,split(concat_ws(' ...

  2. scrapy windows 安装

    windows 7 系统下参照官网安装总是会提示出错,现在整理一下安装的流程 1.安装 python 2.7,添加环境变量 C:\Python27\;C:\Python27\Scripts\; 在 C ...

  3. Hdu4632 Palindrome subsequence 2017-01-16 11:14 51人阅读 评论(0) 收藏

    Palindrome subsequence Problem Description In mathematics, a subsequence is a sequence that can be d ...

  4. linux系统编程之信号(六):信号发送函数sigqueue和信号安装函数sigaction

    一,sigaction() #include <signal.h> int sigaction(int signum,const struct sigaction *act,struct ...

  5. 【WPF】UserControl 的 Load事件

    经过查看MSDN,总结下 UserControl 的 Load 事件: Q1:Load事件什么时候发生? 在控件第一次变为可见之前发生. Load事件发生在创建 UserControl 时,因此有些情 ...

  6. Tasks遇到的一些坑,关于在子线程中对线程权限认证。

    一般情况下,不应该在执行多线程认证的时候对其子线程进行身份认证,如:A线程的子线程B和子线程C. 当使用 Parallel.ForEach方法时,只有自身线程能够拥有相对应的权限,其子线程权限则为NU ...

  7. WPF制作表示透明区域的马赛克画刷

    最近在用WPF制作一款软件,需要像ps一样表示透明区域,于是制作了一个马赛克背景的style.实现比较简单,那么过程和思路就不表了,直接上代码 <DrawingBrush TileMode=&q ...

  8. c# async Task await Result 死锁

    最近项目数据量较大,使用 async Task异步增加执行效率 遇到问题,当前有2个计算非常耗时,现在需要你优化一下,这2个计算并行执行,2个计算执行完成后将2个结果sum返回给用户 当前我是这样实现 ...

  9. 微信退款证书使用c#

    微信退款需要证书 data为已封装好的xml数据 具体怎么封装>打开 public string get(string data) { string cert = @"D:\certi ...

  10. JS 中的数据类型

    简介 JavaScript 语言的每一个值,都属于某一种数据类型.JavaScript 的数据类型,共有七种 数值(number):整数和小数,比如1和3.14 字符串(string):文本 布尔值( ...