1 / 15

Inner Classes

Inner Classes. Nested Classes. class TheEnclosingClass{ . . . class ANestedClass { . . . } }. Definition : A nested class is a class that is a member of another class. Reason for making nested classes :

Télécharger la présentation

Inner Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inner Classes

  2. Nested Classes classTheEnclosingClass{ . . . classANestedClass { . . . } } • Definition: A nested class is a class that is a member of another class. • Reason for making nested classes: • the nested classmakes sense only in the context of its enclosing class • the nested classneeds the enclosing class to have the right functionality.

  3. Nested Static Classes • A nested class that is declared static is attached to the enclosing class and not to objects of the enclosing class. Instance fields and methods can not be directly accessed.

  4. Inner Classes • A nested class that is not static is called an inner class. • An inner class is associated with an object of its enclosing class and it has direct and unlimited access to that object's instance variables and methods. • A nested class can be declared at the top level inside a class or inside any block of code.

  5. Example: Linked List public class LinkedList { private Node first; ..... public static class Node{ public Node next; public Object data; } ..... }

  6. Inner Class - Example interface Property{ public String getVariable(); public void setVariable( String newValue); } Utskriften blir: variable: Old Value! variable: New value! class InnerClassApplet extends Applet { //Initialize the applet public void init() { Utter obj = new Utter(); Property propertyObject = obj.getInnerClassObject(); System.out.println("variable: " + propertyObject.getVariable()); propertyObject.setVariable("New value!"); System.out.println("variable: " + propertyObject.getVariable()); } } class Utter{ private String variable = "Old Value!"; private class InnerClass implements Property{ public String getVariable(){ return variable; } public void setVariable(String newValue){ variable=newValue; } } public Property getInnerClassObject(){ return new InnerClass(); } }

  7. ”interface”Property interface Property{ public String get(); public void set(String s); public String name();} +get(): String+set(s: String): void+name(): String Example:Property Editor[1] • The property editor lets you see properties (fields) and change properties for different objects. • A property has a name, it has a value and the value can often be changes. The behavior is given by the Property interface.

  8. PropertyEditor class PropertyEditor{ public PropertyEditor(Property[] p){ properties = p; } public void editProperties() { while (true) { for (int i = 0; i < properties.length; i++){ System.out.println((i + 1) + ":” + properties[i].name() + "=" + properties[i].get()); } int n = Console.readInt( "Change which property? (0 to quit)"); if (n == 0) return; if (0 < n && n <= properties.length) { String value = Console.readString("New value:"); properties[n - 1].set(value); } } } private Property[] properties; } -properties: Property[] +PropertyEditor(p:Property[]): +editProperties(): void Property Typical dialog with the editor: 1: Harry Hacker’s salary=35000.0 2: Carl Cracker’s salary=75000.0 3: Carl Cracker’s years on the job=9 Change which property? (0 to quit)2 New value: 94000 The PropertyEditor

  9. Employee Property Property -name: String -salary:double -hireDay:Day +Employee(name:String,..): +print(): void +raiseSalary(hyPercent:double):void +getSalaryProperty(): Property +getSeniorityProperty():Property ”returns” SeniorityProperty SalaryProperty ”returns” - isSet: boolean ”inner class” ”inner class” +get(): String+set(s: String): void+name(): String +get(): String+set(s: String): void+name(): String can not beset can only beset once for each SalaryPropertyobject A class with Properties

  10. A class with Properties class Employee{ private String name; private double salary; private Day hireDay; public Employee(String n, double s, Day d){name=n; salary=s; hireDay=d;} public void print(){...} public void raiseSalary(double byPercent){....} public int hireYear(){return hireDay.getYear(); } private class SalaryProperty implements Property{ public String name() { return name + "'s salary"; } public String get() { return "" + salary; } public void set(String s){ if (isSet) return; // can set once double sal = Format.atof(s); if (sal > 0){ salary = sal; isSet = true; } } private boolean isSet = false; } public Property getSalaryProperty() { return new SalaryProperty();}

  11. Rest of the Employee class private class SeniorityProperty implements Property{ public String name() { return name + "'s years on the job"; } public String get() { Day today = new Day(); int years = today.daysBetween(hireDay) / 365; return "" + years; } public void set(String s) {} // can't set seniority } public Property getSeniorityProperty() { return new SeniorityProperty(); } }

  12. Test of the Property Editor public class PropertyTest { public static void main(String[] args) { Employee harry = new Employee("Harry Hacker", 35000, new Day(1989, 10, 1)); Employee carl = new Employee("Carl Cracker", 75000, new Day(1987, 12, 15)); PropertyEditor editor = new PropertyEditor( new Property[] { harry.getSalaryProperty(), harry.getSalaryProperty(), carl.getSalaryProperty(), carl.getSeniorityProperty() } ); System.out.println("Before:"); harry.print(); carl.print(); System.out.println("Edit properties:"); editor.editProperties(); System.out.println("After:"); harry.print(); carl.print(); } }

  13. AnonymousInner Classes • It is possible to make objects of inner classes that do not have names. • The syntax: new SuperType(construction parameters){ inner class methods and data } • The SuperType can be an interface or a class, so the anonymous class will be an implementation or a subtype of the SuperType. • The construction parameters is sent to the constructor of the supertype if SuperType is a class.

  14. The SeniortyProperty as an anonymous class public Property getSeniorityProperty(){ return new Property(){ public String name(){ return name + "'s years on the job"; } public String get(){ Day today = new Day(); int years = today.daysBetween(hireDay) / 365; return "" + years; } public void set(String s){ } // can't set seniority } }

  15. References • [1] Cay S. Horstmann and Gary Cornell:Core Java 1.1, Volume I.Prentice Hall, 1997

More Related