[ Pobierz całość w formacie PDF ]
.Since it is the standard naming scheme for Java, the generated files areautomatically platform-independent.(Note that the Java compiler is changingyour inner classes in all sorts of other ways in order to make them work.)[ Add Comment ]Why inner classes?At this point you’ve seen a lot ofsyntax and semantics describing the way inner classes work, but thisdoesn’t answer the question of why they exist.Why did Sun go to so muchtrouble to add this fundamental language feature?[ Add Comment ]Typically, the inner class inherits froma class or implements an interface, and the code in the inner classmanipulates the outer class object that it was created within.So you could saythat an inner class provides a kind of window into the outer class.[ Add Comment ]A question that cuts to the heart ofinner classes is this: if I just need a reference to an interface, whydon’t I just make the outer class implement that interface? Theanswer is “If that’s all you need, then that’s how you shoulddo it.” So what is it that distinguishes an inner class implementing aninterface from an outer class implementing the same interface? Theanswer is that you can’t always have the convenience ofinterfaces—sometimes you’re working with implementations.Sothe most compelling reason for inner classes is:Each inner class can independentlyinherit from an implementation.Thus, the inner class is not limited by whetherthe outer class is already inheriting from animplementation.Without the ability that inner classesprovide to inherit—in effect—from more than one concrete orabstract class, some design and programming problems would beintractable.So one way to look at the inner class is as the completion of thesolution of the multiple-inheritance problem.Interfaces solve part of theproblem, but inner classes effectively allow “multiple implementationinheritance.” That is, inner classes effectively allow you to inherit frommore than one non-interface.[ Add Comment ]To see this in more detail, consider asituation where you have two interfaces that must somehow be implemented withina class.Because of the flexibility of interfaces, you have two choices: asingle class or an inner class://: c08:MultiInterfaces.java// Two ways that a class can// implement multiple interfaces.interface A {}interface B {}class X implements A, B {}class Y implements A {B makeB() {// Anonymous inner class:return new B() {};}}public class MultiInterfaces {static void takesA(A a) {}static void takesB(B b) {}public static void main(String[] args) {X x = new X();Y y = new Y();takesA(x);takesA(y);takesB(x);takesB(y.makeB());}} ///:~Of course, this assumes that thestructure of your code makes logical sense either way.However, you’llordinarily have some kind of guidance from the nature of the problem aboutwhether to use a single class or an inner class.But without any otherconstraints, in the above example the approach you take doesn’t reallymake much difference from an implementation standpoint.Both of them work.[ Add Comment ]However, if you have abstract orconcrete classes instead of interfaces, you are suddenly limited to usinginner classes if your class must somehow implement both of theothers://: c08:MultiImplementation.java// With concrete or abstract classes, inner// classes are the only way to produce the effect// of "multiple implementation inheritance."class C {}abstract class D {}class Z extends C {D makeD() { return new D() {}; }}public class MultiImplementation {static void takesC(C c) {}static void takesD(D d) {}public static void main(String[] args) {Z z = new Z();takesC(z);takesD(z.makeD());}} ///:~If you didn’t need to solve the“multiple implementation inheritance” problem, you could conceivablycode around everything else without the need for inner classes.But with innerclasses you have these additional features:[ Add Comment ]The inner class can havemultiple instances, each with its own state information that is independent ofthe information in the outer class object.[ Add Comment ]Ina single outer class you can have several inner classes, each of which implementthe same interface or inherit from the same class in a different way.Anexample of this will be shown shortly.[ Add Comment ]Thepoint of creation of the inner class object is not tied to the creation of theouter class object.[ Add Comment ]Thereis no potentially confusing “is-a” relationship with the innerclass; it’s a separate entity.[ Add Comment ]Asan example, if Sequence.java did not use inner classes, you’d haveto say “a Sequence is a Selector,” and you’donly be able to have one Selector in existence for a particularSequence.Also, you can have a second method,getRSelector( ), that produces a Selector that moves backwardthrough the sequence.This kind of flexibility is only available with innerclasses.[ Add Comment ]Closures & CallbacksA closure is a callable objectthat retains information from the scope in which it was created
[ Pobierz całość w formacie PDF ]