Recipe16.1.Extending an Existing Class


Recipe 16.1. Extending an Existing Class

Problem

You want to extend an existing class.

Solution

Declare the new methods and attributes to be added to the existing class within the aspect.

Discussion

Example 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 class
public 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 method


See Also

Recipe 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.



AspectJ Cookbook
Aspectj Cookbook
ISBN: 0596006543
EAN: 2147483647
Year: 2006
Pages: 203
Authors: Russ Miles

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net