Passing Derived class as a parameter to a method when the parameter type is base class

前端 未结 4 2275
暖寄归人
暖寄归人 2020-12-05 04:20

I am a newbie and trying to understand concepts of inheritance and design patterns.

I came across this pattern http://en.wikipedia.org/wiki/Strategy_pattern when I w

4条回答
  •  粉色の甜心
    2020-12-05 04:38

    Put really simply:

    A Derived class (or subclass) is an instance of its base class.

    So, when you pass an instance of ConcreteStrategyAdd into the constructor, you are essentially passing a Strategy object in.

    There is no casting involved. The type hierarchy allows for this type of programming. It allows programmers to use polymorphism in their code.

提交回复
热议问题