Java-不可以访问foo类型的封闭实例我有以下代码:class Hello {
class Thing {
public int size;
Thing() {
size = 0;
}
}
public static void main(String[] args) {
Thing thing1 = new Thing();
System.out.println("Hello, World!");
}}我知道呀Thing什么也不做,但我的你好,世界程序编译只是很好没有它。只有我定义的类在我身上失败了。它拒绝编译。我得到No enclosing instance of type Hello is accessible."在创造新事物的那条线上。我猜要么是:我有系统级别的问题(无论是在DrJava中还是在我的Java安装中),或者对于如何用java构建一个工作程序,我有一些基本的误解。有什么想法吗?
3 回答
慕斯709654
TA贡献1840条经验 获得超5个赞
static class Thing
ThingHellonew Thing();Hello
Hello
人到中年有点甜
TA贡献1895条经验 获得超7个赞
ThingHello
Thing
移动 Thing走出 Hello班级,等级。 变化 Thing成为 static嵌套类 static class Thing
创造 实例的 Hello在创建 Thing.public static void main(String[] args){ Hello h = new Hello(); Thing thing1 = h.new Thing(); // hope this syntax is right, typing on the fly :P}
ThingHello
public class Hello {
public int enormous;
public Hello(int n) {
enormous = n;
}
public class Thing {
public int size;
public Thing(int m) {
if (m > enormous)
size = enormous;
else
size = m;
}
}
...}Thing
Thing t = new Thing(31);
enormoushHelloh.enormous
...Hello h = new Hello(30);...Thing t = h.new Thing(31);...
ThingHello.
添加回答
举报
0/150
提交
取消
