C #, I got this during a programming interview

During a recent interview, I was asked to write a program that contains a list of different vehicles or something else that implements the honk interface, uses an abstract class, and then has a different hone for different vehicles. This is what I came up with and works fine as long as I call the methods myself. But when I try to put them in IEnumerable and then scroll through it, it displays the hone for the abstract class, not the individual classes. Can someone explain what I'm doing wrong?

    using System;
    using System.Collections.Generic;

    namespace ConsoleHonk
    {
        class Program
        {
            static void Main(string[] args)
            {
                var myList = GetVehicles();

                //This doesn't display the correct honk
                myList.ForEach(x => x.honk());
            }

            private static List<IHonker> GetVehicles()
            {
                var myList = new List<IHonker>();    

                var myTruck = new Truck();
                var myCar = new Car();
                var myGoose = new Goose();

                myList.Add(myTruck);
                myList.Add(myGoose);
                myList.Add(myCar);

                return myList;
             }
         }

         class Goose : HonkClass
         {
             public virtual void honk()
             {
                 Console.WriteLine("Quack");
             }
         }

         class Car : HonkClass
         {

         }

         class Truck:HonkClass
         {
              public virtual void honk()
             {
                  Console.WriteLine("Honk-Honk");
             }
         }

         interface IHonker
         {
              string horn { get; set; }
              void honk();
         }

         public abstract class HonkClass:IHonker
         {
             public void honk()
             {
                  Console.WriteLine("Beep");
             }

             public string horn { get; set; }
         }
    }
+4
source share
1 answer

, . , , , , . .

, . , # , . , IHonkable, ISteerable, HonkableBase SteerableBase.

, Honk , . - , :

public class HidingVehicle : HonkClass
{
    public void Honk()
    {
        Console.Writeline("Hiding honk!");
    }
}

public class OverridingVehicle : HonkClass
{
    public override void Honk()
    {
        Console.Writeline("Overriding honk!");
    }
}

public class HonkClass
{
    public virtual void Honk()
    {
        Console.Writeline("Base honk!");
    }
}

:

var myHidingVehicle = new HidingVehicle();
var myOverridingVehicle = new OverridingVehicle();
myHidingVehicle.Honk();  //"Hiding honk!"
myOverridingVehicle.Honk(); //"Overriding honk!"

HonkClass hiddenVehicle = myHidingVehicle;
HonkClass overridenVehicle = myOverridingVehcile;
hiddenVehicle.Honk(); //"Base honk!"
overridenVehicle.Honk(); //"Overriding honk!"

, overriding.

, , . , , , new , -, , .

+4

Source: https://habr.com/ru/post/1540824/


All Articles