Rozmiar: 8938 bajtów


Inner class



An inner class is a class nested within another class (computer science). Inner classes became a feature of the Java programming language starting with version 1.1. They are also a feature of the D programming language. ==Overview== An instance of a normal or top-level class can exist on its own. By contrast an instance of an inner class cannot be instantiated without a top-level class to bind to. Let us take the abstract notion of a Car with four wheels. Our wheels have a specific feature that relies on being part of our Car. This notion does not represent the wheels as wheels in a more general form that could be part of vehicle. Instead it represents them as specific to this one. We can model this notion using inner classes as follows: We have the top-level class Car. Instances of Class Car are composed of four instances of the class Wheel. Remember, this particular implementation of Wheel is specific to the car and therefore the code does not model the general notion of a Wheel which would be better represented as a top-level class. Therefore it is semantically connected to the class Car and the code of Wheel is in some way coupled to its outer class. Inner classes provide us with a mechanism to accurately model this connection this in Java. We say that our wheel class is Car.Wheel. Car being the top-level class and Wheel being the inner class. Inner classes therefore allow for the object orientation of certain parts of the program that would otherwise not be encapsulated into a class. Larger segments of code within a class might be better modelled or refactored as a separate top-level class, rather than an inner class. This would make the code more general in its application and therefore more re-usable but potentially might be premature generalisation. This may prove more effective if code has many inner classes with shared functionality. ==Types of inner class== In Java there are four types of inner class: *Static member classes *Member classes *Local classes *Anonymous classes ==GUI code== Local inner classes are often used in Java to define callbacks for GUI code. Components can then share an object that implements an event handling interface or extends an abstract adapter class, containing the code to be executed when a given event is triggered. Anonymous inner classes are also used where the event handling code is only used by one component and therefore does not need a named reference. This avoids a large monolithic actionPerformed() methods with multiple if else branches to identify the source of the event. This type of code is often considered messy and the inner class variations are considered to be better in all regards. ==Disadvantages== ==External Links== [http://c2.com/cgi/wiki?InnerClasses c2c.com wiki: Inner Classes] [http://www.digitalmars.com/d/class.html#nested D nested classes]


See other meanings of words starting from letter:

I

IA | IB | IC | ID | IE | IF | IG | IH | IJ | IK | IL | IM | IN | IO | IP | IR | IS | IT | IU | IW | IX | IY | IZ |

Words begining with Inner_class:

Inner_class


These materials are based on Wikipedia and licensed under the GNU FDL



YouTube.com videos better site than Turbo Tax 2007
encyklopedia online