简体   繁体   中英

Inheritance: methods

Given the following code:

class BaseClass
{
    protected void Do(var param) {}; 
}

class DerivedClass
{
     public 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); 
   }
}

如果good =]可以,是的,这里的组合可能不会破坏封装,但是这似乎是对惯性的可接受使用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM