要在定义内部类的类的外部创建一个内部类的对象,不能用下面的方法
public class Main{
public static void main(String[] args){
//TestInnerClass tic=new TestInnerClass();
TestInnerClass.Inner in=new TestInnerClass.Inner(); //compile error
//TestInnerClass.Inner in=tic.new Inner();
}
}
class TestInnerClass{
class Inner{
public Inner(){
System.out.println("Inner constructor");
}
}
}
而要这样
public class Main{
public static void main(String[] args){
TestInnerClass tic=new TestInnerClass();
TestInnerClass.Inner in=tic.new Inner();
}
}
class TestInnerClass{
class Inner{
public Inner(){
System.out.println("Inner constructor");
}
}
}
下面的代码并没有创建一个Inner对象
public class Main{
public static void main(String[] args){
TestInnerClass tic=new TestInnerClass();
//TestInnerClass.Inner in=tic.new Inner();
}
}
class TestInnerClass{
class Inner{
public Inner(){
System.out.println("Inner constructor");
}
}
}