Java的Inner Class的实例及作用分析
来源:互联网 发布:剑灵如何导入捏脸数据 编辑:程序博客网 时间:2024/04/29 22:37
http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html
1 首先看代码
/**//*
* DataStructure.java
*
* Created on 2008年3月1日, 上午10:37
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package hiber;
/** *//**
*
* @author rulinma
*/
public class DataStructure ...{
//create an array
private final static int SIZE = 15;
private int[] arrayOfInts = new int[SIZE];
public DataStructure() ...{
//fill the array with ascending integer values
for (int i = 0; i < SIZE; i++) ...{
arrayOfInts[i] = i;
}
}
public void printEven() ...{
//print out values of even indices of the array
InnerEvenIterator iterator = this.new InnerEvenIterator();
while (iterator.hasNext()) ...{
System.out.println(iterator.getNext() + " ");
}
}
//inner class implements the Iterator pattern
private class InnerEvenIterator ...{
//start stepping through the array from the beginning
private int next = 0;
public boolean hasNext() ...{
//check if a current element is the last in the array
return (next <= SIZE - 1);
}
public int getNext() ...{
//record a value of an even index of the array
int retValue = arrayOfInts[next];
//get the next even element
next += 2;
return retValue;
}
}
public static void main(String s[]) ...{
//fill the array with integer values and print out only values of even indices
DataStructure ds = new DataStructure();
ds.printEven();
}
}
* DataStructure.java
*
* Created on 2008年3月1日, 上午10:37
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package hiber;
/** *//**
*
* @author rulinma
*/
public class DataStructure ...{
//create an array
private final static int SIZE = 15;
private int[] arrayOfInts = new int[SIZE];
public DataStructure() ...{
//fill the array with ascending integer values
for (int i = 0; i < SIZE; i++) ...{
arrayOfInts[i] = i;
}
}
public void printEven() ...{
//print out values of even indices of the array
InnerEvenIterator iterator = this.new InnerEvenIterator();
while (iterator.hasNext()) ...{
System.out.println(iterator.getNext() + " ");
}
}
//inner class implements the Iterator pattern
private class InnerEvenIterator ...{
//start stepping through the array from the beginning
private int next = 0;
public boolean hasNext() ...{
//check if a current element is the last in the array
return (next <= SIZE - 1);
}
public int getNext() ...{
//record a value of an even index of the array
int retValue = arrayOfInts[next];
//get the next even element
next += 2;
return retValue;
}
}
public static void main(String s[]) ...{
//fill the array with integer values and print out only values of even indices
DataStructure ds = new DataStructure();
ds.printEven();
}
}
2 运行结果:
init:
deps-jar:
compile-single:
run-single:
0
2
4
6
8
10
12
14
生成成功(总时间:1 秒)
3 作用分析
3.1 内部类的使用可以有效避免外界对InnerEvenIterator 类的访问,满足了面向对象的封装性。
3.2 内部类InnerEvenIterator可以直接使用上面定义的 private int[] arrayOfInts = new int[SIZE];
Java编译器在创建内部类对象时,隐式的把其外部类对象的引用也传了进去并一直保存着。这样就使得内部类对象始终可以访问其外部类对象
- Java的Inner Class的实例及作用分析
- C# Inner Class vs. java 的inner class比较
- 实例分析Java Class的文件结构
- 实例分析Java Class的文件结构
- 实例分析 Java Class 的文件结构
- 实例分析Java Class的文件结构
- 实例分析Java Class的文件结构
- 实例分析Java Class的文件结构
- 实例分析Java Class的文件结构
- static inner class和非static inner class的实例化问题
- Inner class 的 static
- Java Class的作用
- Java: 除了Inner Class之外的两种nested Class
- Java中Static Nested Class 和 Inner Class的不同
- Java中Static Nested Class 和 Inner Class的不同
- Java接口的作用及实例
- JAVA反射机制及CLASS.FORNAME的作用及含义
- Java下Class.forName的作用及使用原因
- JAVA进程waitFor() 阻塞总结
- Moss 2007 入门(1) - 功能概述
- MOSS 2007 部署图示指南
- 优秀课件笔记之Geometric Transformations
- 當您嘗試安裝或執行 Office 程式時,可能會收到錯誤訊息:「無法開啟修補封裝」或「這個產品的安裝來源無法使用」
- Java的Inner Class的实例及作用分析
- 优秀课件笔记之2D Viewing
- 优秀课件笔记之信息系统与管理
- nutch注意事项:
- 模式定理
- 卡巴斯基7.0如何设置授权文件
- 优秀课件笔记之管理信息系统
- 优秀课件笔记之管理信息系统的系统分析
- 解密X-Change2的脚本