You can create methods that delegate behavior to the fields or methods of your class. This approach makes it possible to give access to the information of a field or method without direct exposing this field or method.
- On the main menu, choose . Alternatively, right-click the editor and choose Generate on the context menu, or use ⌘N, ⌃⏎, ⌘N or ⌃⏎⌃N, ⌃⏎, ⌃N or ⌃⏎⌘N, ⌃⏎, ⌘N or ⌃⏎^ N, ⌥ Insert, ⌃N or ⌃N⌥ Insert keyboard shortcut.
- In the pop-up list that is displayed in the editor, select Delegate Methods. Select Target To Generate Delegates For dialog appears, displaying the list of objects to be delegated to.
- Select the target field or method, and click OK. The Select Method To Generate Delegation For dialog appears, displaying the list of methods to delegate.
- Select the desired methods. For multiple selection, use ^ and ⇧ keys. Click OK.
Example
Currency
class has a field
calendar
of the type Calendar. To gain access to certain functionality
of the
Calendar
class from the
Currency
class,
we need to create a new method that will delegate the request to calendar.
Calendar calendar; public int get(int i) { return calendar.get(i); }