More about Virtual / new…plus interfaces!

前端 未结 6 928

Yesterday I posted a question about the new/virtual/override keywords, and i learned a lot from your answers. But still i remain with some doubts.

In between all the

6条回答
  •  心在旅途
    2021-01-01 06:07

    From what I understand you're asking, given a subclass with some overridden methods, how to know which method will get called.

    basically, there are 3 options I can think of:

    1. If SubClass does not define a method on BaseClass, then BaseClass's method will get called
    2. If SubClass OVERRIDES a method on BaseClass, then the SubClass's method will get called
    3. If SubClass defines a NEW method which ALSO exists on BaseClass, then the method to get called will depend on the REFERENCE to the object (whether your variable is typed as BaseClass or SubClass)

    I hope I understood your question

    edit: a quick note about interfaces: If BaseClass implements IInterface, then SubClass, which derives from BaseClass, already has the implementation of IInterface and does not need to re-implement it. E.g. if there's an IVehicle with an MPH property, and there's a BaseVehicle that implements that, since Car derives from BaseVehicle, Car already has an MPH property

提交回复
热议问题