In OOP, a term delegation is mentioned. How is this modelled in a class? I searched on yahoo etc but got links to delegates.
相关问题
- how to define constructor for Python's new Nam
- Keeping track of variable instances
- Object.create() bug?
- std::vector of objects / pointers / smart pointers
- Name for a method that has only side effects
相关文章
- 接口B继承接口A,但是又不添加新的方法。这样有什么意义吗?
- NameError: name 'self' is not defined, eve
- Implementation Strategies for Object Orientation
- Check if the Type of an Object is inherited from a
- When to use Interfaces in PHP
- Are default parameters bad practice in OOP?
- How to return new instance of subclass while initi
- Builders in Java versus C++?
Imagine you have the classes
Car
andEngine
:In this example the
Car
delegates to the underlyingEngine
. The user of the car cannot directly start the engine (unless he is a mechanic). But he can tell the car to start, and the car in turn tells the engine to start.You'd want to use it whenever you use object composition and you need to use a method of one of the composing objects. In that case you create a method that delegates to it.
Delegation is like inheritance except instead of class2 having copied functions and variables from class1 class2 just gets class1 to do that stuff for it and class2 focuses on doing and having the extra functions and variables you give it. One obvious advantage of this is it saves space on your computer.