开发者

Inheritance: methods

开发者 https://www.devze.com 2023-02-03 02:51 出处:网络
Given the following code: class BaseClass { protected void Do(var param) {}; } class DerivedClass { public开发者_Go百科 void DoSomething()

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

精彩评论

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