Inner Classes Example
来源:互联网 发布:淘宝开店钱盾认证 编辑:程序博客网 时间:2024/06/08 10:02
Inner Class Example
To see an inner class in use, first consider an array. In the following example, you create an array, fill it with integer values, and then output only values of even indices of the array in ascending order.
The DataStructure.java
example that follows consists of:
- The
DataStructure
outer class, which includes a constructor to create an instance ofDataStructure
containing an array filled with consecutive integer values (0, 1, 2, 3, and so on) and a method that prints elements of the array that have an even index value. - The
EvenIterator
inner class, which implements theDataStructureIterator
interface, which extends theIterator
<
Integer
>
interface. Iterators are used to step through a data structure and typically have methods to test for the last element, retrieve the current element, and move to the next element. - A
main
method that instantiates aDataStructure
object (ds
), then invokes theprintEven
method to print elements of the arrayarrayOfInts
that have an even index value.
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 DataStructureIterator iterator = this.new EvenIterator(); while (iterator.hasNext()) { System.out.print(iterator.next() + " "); } System.out.println(); } interface DataStructureIterator extends java.util.Iterator<Integer> { } // Inner class implements the DataStructureIterator interface, // which extends the Iterator<Integer> interface private class EvenIterator implements DataStructureIterator { // Start stepping through the array from the beginning private int nextIndex = 0; public boolean hasNext() { // Check if the current element is the last in the array return (nextIndex <= SIZE - 1); } public Integer next() { // Record a value of an even index of the array Integer retValue = Integer.valueOf(arrayOfInts[nextIndex]); // Get the next even element nextIndex += 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(); }}
The output is:
0 2 4 6 8 10 12 14
Note that the EvenIterator
class refers directly to the arrayOfInts
instance variable of the DataStructure
object.
You can use inner classes to implement helper classes such as the one shown in the this example. To handle user interface events, you must know how to use inner classes, because the event-handling mechanism makes extensive use of them.
本文来自javase8的官网tutorial,这个小程序主要展示使用Inner Classes的好处。如果不使用Inner Classes,你需要定义一个接口类,然后再另外新建一个外部类,这样就会有三个类了,显然对于实现这样的一个小功能来说,这样做显得麻烦。(根据文档的个人理解,如有错误,欢迎指出)
- Inner Classes Example
- Inner Classes
- Nested Classes和Inner Classes
- Interfaces & Inner Classes
- Closures callbacks Inner Classes
- Java中的Nested Classes和Inner Classes
- 第八章: Interfaces & Inner Classes
- Debug with anonymous inner classes
- Context泄漏:Handlers & Inner Classes
- Nested Classes,Static Classes,Non-static Classes,Inner Classes,Local Classes,Anonymous Classes
- Kotlin-16.嵌套类/内部类(Nested Classes/Inner classes)
- Inner classes in Java, the mystery within.
- Nested, Inner, Member, and Top-Level Classes
- 局部内部类(Local inner classes)
- Inner versus nested classes Java Interview Question
- Gson使用方法之Serializing Inner Classes
- inner classes cannot have static declarations
- Java Google Json (Gson) Serializing Inner classes
- hdu 3507Print Article
- 怎么判断CString的类型是否为空
- windows下Cocos2d-x3.2 + Visual Studio 2012开发环境搭建
- longest common subsequence 问题
- IOS开发概述
- Inner Classes Example
- POJ3498 拆点最大流
- Android解析中国天气网的Json数据
- Linux中断浅析http://bbs.chinaunix.net/thread-3566316-1-1.html
- 进程间通信2
- UVA - 11986 Save from Radiation
- Ubuntu频繁报错
- 求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
- JavaScript省市二级联动