static inner class和非static inner class的实例化问题
来源:互联网 发布:专业制谱软件 编辑:程序博客网 时间:2024/05/16 11:48
package innerclasses; public class E18_NestedClass { static class Nested { void f() { System.out.println("Nested.f"); } } public static void main(String args[]) { Nested ne = new Nested(); ne.f(); } } class Other { // Specifying the nested type outside // the scope of the class: void f() { E18_NestedClass.Nested ne = new E18_NestedClass.Nested(); } } /* Output: Nested.f *///:~
1: static inner class
当在同一个class内实例化内部类的话,只简单new就可以了。
当在另一个class内的化,就需要写清楚包含内部类的类名,如 Class.InnerClass = new Class.InnerClass();
(You can refer to just the class name when inside the method of a class with a defined nested (static inner) class, but outside the class, you must specify the outer class and nested class, as shown in Other, above. )
=================================================================================================='
public class Test20 { public static void main(String[] args) {Class2 c = new Class2();Class2.Inner i = c.new Inner();i.f(); }}class Class2 { class Inner {void f() { System.out.println("Interface1 -> Inner -> f()");} }}
2:非static inner class
http://bbs.bccn.net/thread-168190-1-1.html
First first = new First();
First.Second second = first.new First();
First.Second.Third third = second.new Third();
因为内部类中要保存外部类的一个引用,所以你先要生成外部类的对象。然后通过这个外部类的对象去创建内部类的对象。(static inner class不需要先建立外部对象)
- static inner class和非static inner class的实例化问题
- Inner class 的 static
- static inner class
- non-static inner class
- Static Nested Class 和 Inner Class区别
- Static Nested Class 和 Inner Class区别
- Static Nested Class 和 和 Inner Class 的不同
- static class(inner class中 )
- inner class, static nested class
- Static Nested Class 和 Inner Class的不同
- Inner Class和Static Nested Class的区别
- Java中Static Nested Class 和 Inner Class的不同
- Static Nested Class 和 Inner Class的不同
- Static Nested Class 和 Inner Class的不同
- 【转】Static Nested Class 和 Inner Class的不同
- static nested class和inner class的区别
- Static Nested Class 和 Inner Class的不同
- Static Nested Class 和Inner Class的不同
- Excel列序号生成字母表示
- HQL
- tree的实现 java, 二叉树的实现
- 中断INT 13H的扩展功能
- linux串口驱动分析
- static inner class和非static inner class的实例化问题
- linux 不删除文件,清空文件内容命令
- 获取信息的有关Windows API
- Eclipse C/C++开发环境搭建----Window版
- 单选复选设置为只读灰色
- 解析C语言中的sizeof
- J2ME/J2EE实现用户登录交互
- unix中程序执行时信号的状态
- SWT/Jface ProgressBar-进度条(转载自:《Eclipse SWT/JFACE 核心应用》 清华大学出版社 9.7 进度条(ProgressBar))