Inheritance: methods
Given the following code:
class BaseClass
{
protected void Do(var param) {};
}
class DerivedClass
{
public开发者_Go百科 void DoSomething()
{
var param = something;
Do(param);
}
}
Is this the proper way to use inheritance? I would think I would have put the "Do" method in a separate class and use composition. However, that would violate encapsulation, as I would have to acces an object in the base class:
class DerivedClass
{
public void DoSomething()
{
var param = something;
base.GetObject().Do(param);
}
}
Yep that way if good =] Composition here probably wouldn't break encapsulation, but this seems like an acceptable use of inehritance.
精彩评论