SlideServe
Menu
Préférences
© SlideServe · v2026

Inner Classes

schuyler avatar
11 diapositives · 686 Views · Oct 03, 2012

Inner Classes. An Inner Class is a class defined inside of another class (Static) Inner Classes are a lot like C++ ’ s nested classes Since Java 1.1, Java now has Inner Classes

Inner Classes - slide 1
01 / 11Inner Classes
1 / 17

Télécharger la présentation

Inner Classes
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Inner Classes • An Inner Class is a class defined inside of another class • (Static) Inner Classes are a lot like C++’s nested classes • Since Java 1.1, Java now has Inner Classes • Originally, the Java authors decided not to include them because they thought they would needlessly complicate the language. • However, they can be very useful, especially with AWT (GUI) event handling.

  2. Inner Classes • An Inner class is a class defined inside of another class. Why would anyone want to do that? • An Inner class object can access the implementation of the object that created it – including private fields • Inner classes can be hidden from other classes in the same package • Anonymous inner classes are frequently used for creating event callbacks

  3. How to declare an inner class • You declare an inner class almost like a method inside of a class class ICExample { private class thisIsAnInnerClass //THIS IS THE INNER CLASS { // define a class here } }

  4. Visibility Rules • Inner classes are members of the outer class • Can be declared with any visibility (public, private, etc.) • Can see all members (including private) of outer class • Is in the same package as outer class (outer class can access all non-private inner class members) • Typically, the inner class is declared private and the inner classes members are either public or package visible.

  5. outer keyword • With inner classes, Java defines a outer class reference with each inner class which can be accessed with the outer keyword //this is from the BankAccount class on page 216 of Core Java volume I public void actionPerformed(ActionEvent e) //inside the inner class { double interest=outer.balance * this.interestRate; outer.balance += interest; } • The balance field is a private member of the outer class.

  6. Static inner classes • Objects of a static inner class can exist even without an object of the enclosing outer class. • Because of this, a static inner class is not allowed to access fields of the outer object

  7. Local inner classes • You can actually declare an inner class inside of a method, just like you could declare a local variable. • Local classes do not get an access modifier – they are automatically restricted to the method they are defined in • Can only refer to final members of the enclosing class

  8. Anonymous Inner classes • When using a local inner class, if you only want to make one instance of it, you don’t even need to give it a name • This is known as an anonymous inner class • These are convenient for event programming • However, the syntax is extremely cryptic. Look at this example

  9. Anonymous Inner classes public void start(final double rate) { ActionListener adder = new ActionListener() { public void actionPerformed(ActionEvent evt) { double interest = balance * rate / 100; balance += interest; } }; Timer t = new Timer(1000, adder); t.start(); • This is saying, construct a new object of a class that implements the ActionListener interface, where the one required method (actionPerformed) is defined inside the brackets.

  10. Anonymous Inner classes • You have to look very carefully to see a difference between construction of a new object, and construction of a new inner class extending a class. //A person object Person queen=new Person(“Mary”); //Person Object //An object of an inner class extending Person Person count = new Person(“Dracula”) { //class code here};

  11. Anonymous Inner classes • Anonymous Inner classes cannot have constructors, since constructors have to have the same name as the class, and these classes have no names. • As you can see, the syntax for these is confusing – both for people writing and reading the code. Use this with care, if at all.