Generating Delegation Methods
On this page:
Introduction
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.
Creating delegation methods
To create a delegation method
- Do one of the following:
- On the main menu, choose .
- Right-click the editor and choose Generate on the context menu
- Press Alt+Insert.
- 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 Ctrl and Shift 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);
}
Last modified: 6 March 2018