我有以下代码:
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!");
}
}
Run Code Online (Sandbox Code Playgroud)
我知道Thing什么都不做,但我的Hello,World程序在没有它的情况下编译得很好.这只是我定义的类失败了.
它拒绝编译.我开始No enclosing instance of type Hello is accessible."创造一个新的东西.我猜是:
有任何想法吗?
我尝试实例化以下Java代码中定义的内部类:
public class Mother {
public class Child {
public void doStuff() {
// ...
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我试图像这样得到一个Child的实例
Class<?> clazz= Class.forName("com.mycompany.Mother$Child");
Child c = clazz.newInstance();
Run Code Online (Sandbox Code Playgroud)
我得到这个例外:
java.lang.InstantiationException: com.mycompany.Mother$Child
at java.lang.Class.newInstance0(Class.java:340)
at java.lang.Class.newInstance(Class.java:308)
...
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
是否可以使用Java反射从另一个类实例化私有内部类.例如,如果我采用此代码
public class Main {
public static void main(String[] args) {}
}
class OtherClass {
private class Test {}
}
Run Code Online (Sandbox Code Playgroud)
是否可以从类main中的main方法实例化并获得对Test的访问权限.
这是一个Java内部类问题,代码如下所示:
public class Load {
/*
static { //load
System.loadLibrary("lvb");
}*/
public class FILTER_T{
HP05_T hp;
LP40_T lp;
NOTCH50_T notch;
// Load.FILTER_T.HP05_T hp;
// Load.FILTER_T.LP40_T lp;
// Load.FILTER_T.NOTCH50_T notch;
public class HP05_T {
public int[] buf;
public long y1;
public long y2;
public int ptr;
}
public class LP40_T {
public int[] buf;
public int ptr;
}
public class NOTCH50_T {
public int[] buf;
public int ptr;
}
}
public native void func1(FILTER_T filter);
public native int func2(FILTER_T filter, int …Run Code Online (Sandbox Code Playgroud)