Download presentation
Presentation is loading. Please wait.
1
Inner Classes, 1 Inner Classes
2
Inner Classes, 2 Nested Classes nested classDefinition: A nested class is a class that is a member of another class. Reason for making nested classes: makes sense only in the context of its enclosing class –the nested class makes sense only in the context of its enclosing class needs the enclosing class to have the right functionality –the nested class needs the enclosing class to have the right functionality. class TheEnclosingClass{... class ANestedClass {... } class TheEnclosingClass{... class ANestedClass {... }
3
Inner Classes, 3 Nested Static Classes is attached to the enclosing classA 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, 4 Inner Classes A nested class that is not static is called an inner class. inner class is associated with an object direct and unlimited access to that object's instance variables and methods 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. nested classdeclared attop level or inside any block of code A nested class can be declared at the top level inside a class or inside any block of code.
5
Inner Classes, 5 Example: Linked List public class LinkedList { private Node first;..... public static class Node{ public Node next; public Object data; }..... } public class LinkedList { private Node first;..... public static class Node{ public Node next; public Object data; }..... }
6
Inner Classes, 6 Inner Class - Example interface Property{ public String getVariable(); public void setVariable( String newValue); } interface Property{ public String getVariable(); public void setVariable( String newValue); } 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(); } } 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(); } } Utskriften blir: variable: Old Value! variable: New value!
7
Inner Classes, 7 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. ”interface” Property +get(): String +set(s: String): void +name(): String interface Property{ public String get(); public void set(String s); public String name(); }
8
Inner Classes, 8 The Property Editor 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 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 PropertyEditor -properties: Property[] +PropertyEditor(p:Property[]): +editProperties(): void Property 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; } 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; }
9
Inner Classes, 9 A class with Properties Employee -name: String -salary:double -hireDay:Day +Employee(name:String,..): +print(): void +raiseSalary(hyPercent:double):void +getSalaryProperty(): Property +getSeniorityProperty():Property can only be set once for each SalaryProperty object can not be set SeniorityProperty +get(): String +set(s: String): void +name(): String Property ”inner class” ”returns” SalaryProperty - isSet: boolean +get(): String +set(s: String): void +name(): String Property ”inner class” ”returns”
10
Inner Classes, 10 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();} 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();} A class with Properties
11
Inner Classes, 11 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(); } 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(); } Rest of the Employee class
12
Inner Classes, 12 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(); } } 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(); } } Test of the Property Editor
13
Inner Classes, 13 Anonymous Inner 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
Inner Classes, 14 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 } 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 } The SeniortyProperty as an anonymous class
15
Inner Classes, 15 References [1] Cay S. Horstmann and Gary Cornell: Core Java 1.1, Volume I. Prentice Hall, 1997
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.