Slight misunderstanding of example in msdn related to virtual / override

When reading about polymorphism on MSDN, I saw an example of virtual and overridden methods:

public class BaseClass
{
    public virtual void DoWork() { }
    public virtual int WorkProperty
    {
        get { return 0; }
    }
}
public class DerivedClass : BaseClass
{
    public override void DoWork() { }
    public override int WorkProperty
    {
        get { return 0; }
    }
}

DerivedClass B = new DerivedClass();
B.DoWork();  // Calls the new method.

BaseClass A = (BaseClass)B;
A.DoWork();  // Also calls the new method.

What do I want to know in which scenario will someone do this? I just don't see how useful this is. Can someone please give a real example?

+4
source share
3 answers

This is useful when you need a reference to some objects, and you cannot store references to their exact type. If you, for example, have a list of objects of mixed types:

List<BaseClass> list = new List<BaseClass>() {
  new BaseClass(),
  new DerivedClass(),
  new BaseClass(),
  new BaseClass(),
  new DerivedClass(),
  new DerivedClass()
};

You now have a list of links BaseClass, but some of them point to instances DerivedClass.

, . DoWork, BaseClass.DoWork BaseClass instanced, DerivedClass.DoWork DerivedClass:

foreach (BaseClass b in list) {
  b.DoWork();
}
+3

, , Dancer. Dancer, , - , . .

, , , . , , , .

List<Dancer> danceFloor = new List<Dancer>();
danceFloor.Add(new ReservedDancer());
danceFloor.Add(new SuperFreakDancer ());

public class Dancer
{
    public virtual void DoYourDance()
    {
        // do the robot. Everyone knows that one right?
    }
}

public class ReservedDancer : Dancer
{
    public override void DoYourDance()
    {
        // do the waltz
    }
}

public class SuperFreakDancer : Dancer
{
    public override void DoYourDance()
    {
        // breakdance !!!
    }
}

, , Dancer factory ( Dancer, DancerType , ):

public static Dancer NewDancerFromType(DancerType type)
{
    Dancer ret = null;
    switch (type)
    {
        case DancerType.Reserved:
            ret = new ReservedDancer();
            break;
        case DancerType.SuperFreak:
            ret = new SuperFreakDancer();
            break;
    }
    return ret;
}

, Dancer public, private. , factory , Dancer. , , .

+2

, /, - , , , , , . , :

, if-else. :

class Car {
    public virtual void Drive() {
        Console.WriteLine("Driving like a normal car");
    }
}

class RaceCar : Car { 
    public override void Drive() { 
        Console.WriteLine("Driving really fast!");
    }
}

, (pre-OO) , - , :

if (isRaceCar) {
        Console.WriteLine("Driving really fast!");
} else {
        Console.WriteLine("Driving like a normal car");
}

, , .

, :

class OldCar : Car {
     public override void Drive() { 
         Console.WriteLine("Driving very slowly");
     }
}

, -

switch (carType) {

    case RaceCar: ... 
    case OldCar: ...
    case Car: ...
}

, . , , , . GetIn, GetOut, Stop, DoMaintenance .. .

, , , , -, . , , , , .

, if/else switch , , .

, , :

  • (, - Car , ). , , .
  • (, , , , - ).
+2

All Articles