Has the following structure:Code:
The applyeditable of parent is a virtual method. Child must implement the method differently, that is, override the applyeditable method.
In the above Code, it is obvious that parenta and parentb have repeated code. However, because C # does not support multi-inheritance, the repeated Code cannot be encapsulated in the Multi-inheritance mode.
We only have to follow the flexible path: After my practice, we use the hybrid + interface method. It is better to implement multi-inheritance.
Let's look at the figure directly:
The specific steps are as follows:
- Encapsulate the common code in parenta and parentb in basec and use it as an attribute of parent.
- Define the method applyeditable to be rewritten to an interface ibasecaction. (In fact, if there is only one method that needs to be rewritten, you do not need to define an interface. You can simply define the delegate variable of an action in basec. Of course, defining interfaces makes it easier to support multiple rewrite methods .)
- Let childa and childb implement the ibasecaction interface and implement different codes of applyeditable respectively.
- In the constructor of the Child class, pass yourself to the basec for instantiation, that is, this. basec = new basec (this );
I will not post code, completeSource codeFor: http://cid-f73516baeac50992.skydrive.live.com/self.aspx/SharedCode/MultiDerived.zip