public class TestClass {
//......
class InnerClass {
InnerClass() {
}
}
//...... public static void main(String[] args) { TestClass tc = new TestClass();//理解这个没问题。
tc.new InnerClass();//咋是这样写呢?
/**
* 能不能帮我解释一下;不太好理解。
* "." 和 "new" 哪个先处理(也就是顺序啦)
*
* 下面是错误的,(起先我是认为要这么new的,
* //package p does not exist
* new tc.InnerClass(); //编译器把tc 做类名了 ft!
* 错误提示的有道理;至少要写成下面那种形式;我马上反应过来;
*
* //an enclosing instance that contains Parcel2.Contents is required
* new TestClass.InnerClass();
* 理解不了了。什么“一个封装的实例 那个 包含 Parcel2.Contents 是需要的"
* 需要一个包含 Parcel2.Contents 的实例? 那就是tc 啊。
*
*/
}
}
//......
class InnerClass {
InnerClass() {
}
}
//...... public static void main(String[] args) { TestClass tc = new TestClass();//理解这个没问题。
tc.new InnerClass();//咋是这样写呢?
/**
* 能不能帮我解释一下;不太好理解。
* "." 和 "new" 哪个先处理(也就是顺序啦)
*
* 下面是错误的,(起先我是认为要这么new的,
* //package p does not exist
* new tc.InnerClass(); //编译器把tc 做类名了 ft!
* 错误提示的有道理;至少要写成下面那种形式;我马上反应过来;
*
* //an enclosing instance that contains Parcel2.Contents is required
* new TestClass.InnerClass();
* 理解不了了。什么“一个封装的实例 那个 包含 Parcel2.Contents 是需要的"
* 需要一个包含 Parcel2.Contents 的实例? 那就是tc 啊。
*
*/
}
}
--------------
sorry。提示写错了。
是: an enclosing instance that contains TestClass.InnerClass is required
it is not possible to create an object of the inner class unless you already have an object of the outer class
java.awt.geom.Line2D.Double
可以看看jdk的写法:public class TestClass {
public static class InnerClass {
public InnerClass() {
}
} public static void main(String[] args) {
TestClass tc = new TestClass();
new TestClass.InnerClass();
}
}
也就是说方法 new TestClass.InnerClass(); 是一定错的了。
不过我还是不理解:tc.new InnerClass(); 的奥妙所再。
能解释一下么?
我在study;我要清楚how to do. 不是 possible不possible; 0_-!!
关于为什么非静态内部类必须持有外面类的某个实例的应用,这是设计模式的问题了
我在coding的时候刚领悟到。呵呵。
ok。我知道了。结!