开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜