The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
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:DataStructure.java
示例包括:
DataStructure
outer class, which includes a constructor to create an instance of DataStructure
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.DataStructure
外部类,包括一个构造函数,用于创建包含填充了连续整数值(0、1、2、3等)的数组的DataStructure
实例,以及一个打印具有偶数索引值的数组元素的方法。EvenIterator
inner class, which implements the DataStructureIterator
interface, which extends the Iterator<
Integer>
interface.EvenIterator
内部类,实现DataStructureIterator
接口,扩展了Iterator<
Integer>
接口。main
method that instantiates a DataStructure
object (ds
), then invokes the printEven
method to print elements of the array arrayOfInts
that have an even index value.main
方法,实例化一个DataStructure
对象(ds
),然后调用print偶数方法来打印具有偶数索引值的数组arrayOfInts
的元素。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.EvenIterator
类直接引用DataStructure
对象的arrayOfInts
实例变量。
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.要处理用户界面事件,您必须知道如何使用内部类,因为事件处理机制广泛使用它们。
There are two additional types of inner classes.还有两种附加类型的内部类。You can declare an inner class within the body of a method.可以在方法体中声明内部类。These classes are known as local classes.这些类称为局部类。You can also declare an inner class within the body of a method without naming the class.您还可以在方法体中声明内部类,而无需命名该类。These classes are known as anonymous classes.这些类称为匿名类。
You can use the same modifiers for inner classes that you use for other members of the outer class.对于内部类,可以使用与外部类的其他成员相同的修改器。For example, you can use the access specifiers 例如,可以使用访问说明符private
, public
, and protected
to restrict access to inner classes, just as you use them to restrict access do to other class members.private
、public
和protected
来限制对内部类的访问,就像使用它们来限制对其他类成员的访问一样。