再学Java 之 解决No enclosing instance of type * is accessible
深夜,临睡前写了个小程序,出了点小问题
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的更多相关文章
- 解决No enclosing instance of type * is accessible
写一个内部类,并在构造函数中初始化时,遇到报错,搜索问题后发现,有网友出现过类似的问题,下面这个是说的浅显明白的,并确实解决了问题.于是,以下内容照搬过来,不再多费键盘了. public class ...
- 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 ...
- java中"no enclosing instance of type * is accessible"的解决方法
这种情况一般发生在“在静态方法里面使用内部类” 测试代码: public class Test { public static void main(String[] args) { A a = new ...
- 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 ...
- 再学Java 之 interface的成员变量
前言:最近在学多线程,写“哲学家就餐问题(Dining Philosophers)”的时候,需要定义一个全局的变量,即哲学家的人数.常用的做法是在其中一个类中定义一个static final的变量,然 ...
- Java中的static(1)【持续更新】——关于Eclipse的No enclosing instance of type ... 错误的理解和改正
No enclosing instance of type SomeClass 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
最近在看Java,在编译写书上一个例子时,由于书上的代码只有一部分,于是就自己补了一个内部类.结果编译时出现:No enclosing instance of type E is accessible ...
- 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 ...
- 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,在编译写书上一个例子时,由于书上的代码只有一部分,于是就自己补了一 ...
随机推荐
- Linux服务器部署系列之五—Webmin篇
对于很多习惯使用windows的用户,在刚接触Linux的时候,要使用命令行配置Linux服务器可能会感觉难以适应.今天我们来讲解一下,Linux下的图形配置工具—Webmin,通过这款工具,用户可以 ...
- GoogleStyle格式化代码
<div class="iteye-blog-content-contain" style="font-size: 14px"></div&g ...
- Nvidia显卡驱动下载
https://www.nvidia.cn/Download/index.aspx?lang=cn https://blog.csdn.net/weixin_39643690/article/deta ...
- Android传感器——加速度传感器
步骤如下: 1. 调用Context的getSystemService(Context.SENSOR_SERVICE)方法获取SensorManager,SensorManager对象代表系统的传感器 ...
- [转] libcurl异步方式使用总结(附流程图)
文为转载,原文地址:libcurl异步方式使用总结 实习期间用到了libcurl来做HTTPS双向认证,用的是异步方式,简单总结一下. libcurl这个库的同步方式很简单,不做介绍,而异步方式很难理 ...
- http://www.cnblogs.com/xalion/p/5111279.html
https://quality.embarcadero.com/secure/Dashboard.jspa 注册成 delphi qc
- Sublime Text 3 格式化HTML CSS JS 代码
一,首先通过ctrl+shift+p 要等一会就会出现插件安装界面 二,在插件安装输入框,输入:HTML-CSS-JS Prettify 并安装该插件 三,如果没有装nodejs, 下载nodejs ...
- CentOS ASP.NET Core Runtime Jexus跨平台布署
.net core 开源和跨平台,能布署到当前主流的Windows,Linux,macOS 系统上.本篇我们将在 Linux 系统上使用 ASP.NET Core Runtime 和 Jexus 布署 ...
- 利用ligerUI隐藏某列,并不产生空白列的方法
var grid;//声明变量 $(function () { //grid初始化 grid = $("#maingrid4").ligerGrid({ columns: [ { ...
- 使用css保持一定宽高比例
需求描述:移动端实现横跨页面半圆.(类似问题,实现4x4的正方形网格) 简化问题,我们可以理解为实现一个高度和宽度比为1:2的块. 需要解决问题: 1,高度和宽度按照一定比例. 2,外容器高度和宽度不 ...