Recipe 16.1. Extending an Existing ClassProblemYou want to extend an existing class. SolutionDeclare the new methods and attributes to be added to the existing class within the aspect. DiscussionExample 16-1 shows how an attribute and a method can be introduced to the MyClass class. Example 16-1. Adding an attribute and a method to an existing classpublic aspect ExtendClassRecipe { private int MyClass.newVariable = 20; public int MyClass.bar(String name) { System.out.println("In bar(String name), name:" + name); return this.newVariable; } } Figure 16-1 shows the class structure of the MyClass class before and after the aspect in Example 16-1 is applied. Figure 16-1. The MyClass class before and after the aspect introduces a new attribute and methodSee AlsoRecipe 16.2 shows how to specify a new inheritance relationship between classes; Recipe 16.3 shows how to apply new interfaces to an existing class using aspects. |