Java internal classes can obtain the access permissions of all the members of their peripheral classes, and can conveniently perform operations on peripheral classes. Non-static internal classes contain a hidden external class reference, which can be obtained through outerclass. This. Generally, if a group of Operations follow certain rules (such as interfaces and abstract classes), you can encapsulate these operations into an internal class for external use, instead of the peripheral class. See the following code:
Public interface selector <E> {Boolean hasnext (); e next ();} public class sequence {private int [] seq; private selector <integer> obverseselector; // The forward traversal private selector <integer> reverseselector; // The Reverse traversal public sequence (INT [] SEQ) {This. SEQ = seq; obverseselector = new selector <integer> () {int counter =-1; @ override public integer next () {return sequence. this. SEQ [Counter] ;}@ override public Boolean hasnext () {return (++ counter <sequence. this. seq. length) ;}}; reverseselector = new selector <integer> () {int counter = sequence. this. seq. length; @ override public integer next () {return sequence. this. SEQ [Counter] ;}@ override public Boolean hasnext () {return (-- counter >=0) ;};} public selector <integer> getobverseselector () {return obverseselector ;} public selector <integer> getreverseselector () {return reverseselector;} public class test {public static void main (string ARGs []) {int [] A = {1, 2, 3, 4, 5}; Sequence sequence = new sequence (a); selector <integer> obselector = sequence. getobverseselector (); selector <integer> reselector = sequence. getreverseselector (); While (obselector. hasnext () {system. out. print (obselector. next () + "");} system. out. println (); While (reselector. hasnext () {system. out. print (reselector. next () + "");}}}
In this example, the selector interface is implemented through an internal class, so that the sequence class has its own forward and backward traversal servers. Note: To access the members of the peripheral class, use the outer. This prefix to access the anonymous internal class.
It can be imagined that there will be any difference in implementing the traversal without an internal class. If we do not need an internal class to implement it, we must define a specific selector class, in addition, the seq array of the sequence class is passed as a parameter. If selector needs to be used as a shared traversal, this can be done. However, if the traversal is implemented for the sequence class, it is more convenient to use the internal class.
The following code is a selector implemented using a non-Anonymous internal class.
public class Sequence2 { private int[] seq; public Sequence2(int[] seq) { this.seq = seq; } public class ObverseSelecter implements Selector<Integer> { int counter = -1; @Override public Integer next() { return seq[counter]; } @Override public boolean hasNext() { return (++counter < seq.length); } } public class ReverseSelecter implements Selector<Integer> { int counter = seq.length; @Override public Integer next() { return seq[counter]; } @Override public boolean hasNext() { return (--counter >= 0); } }}public class Test { public static void main(String args[]) { int[] a = {1, 2, 3, 4, 5}; Sequence2 sequence2 = new Sequence2(a); Selector<Integer> obSelector2 = sequence2.new ObverseSelecter(); Selector<Integer> reSelector2 = sequence2.new ReverseSelecter(); while (obSelector2.hasNext()) { System.out.print(obSelector2.next() + " "); } System.out.println(); while (reSelector2.hasNext()) { System.out.print(reSelector2.next() + " "); } }}
Note that the new syntax sequence2.new is used to generate traversal. It is easy to see that instances of internal classes are closely related to instances of peripheral classes, just as outer of anonymous internal classes. this syntax is the same
It is worth mentioning that if you want to use a local variable for a local internal class, the local variable must be final. Otherwise, the compilation will fail. See the following code.
public interface Flyable { void fly();}public class FinalTest { public static Flyable getFlyable(int speed) { final int finalSpeed = speed * 2; return new Flyable() { @Override public void fly() { System.out.println("I can fly with speed " + finalSpeed + " km/h"); } }; } public static void main(String[] args) { Flyable flyable = getFlyable(50); flyable.fly(); }}
It can be seen that finalspeed is the final variable. Why is this requirement?
It is because the lifecycle of a local variable is only within the scope of the method execution. When the method execution is complete, the local variable is naturally destroyed. However, the internal class is returned as an instance. If the internal class instance still needs to access the destroyed local variable, it is obviously not feasible. Therefore, local variables to be accessed in the internal class are defined as constants and compiled into the internal class together.