C # - Interface -Help in interface power

I am new to C #. I recently read an article. He offers

<i> "One of the practical uses of an interface is to create an interface reference that can

work with various kinds of objects that implement this interface. "

Based on what I tested (I'm not sure my understanding is correct)

 namespace InterfaceExample

 {
public interface IRide

{

  void Ride();

}

abstract class Animal

{
  private string _classification;

  public string Classification
  {
   set { _classification = value;}
   get { return _classification;}
  }

  public Animal(){}

  public Animal(string _classification)
  {
    this._classification = _classification;
  }

}

class Elephant:Animal,IRide

{

  public Elephant(){}

   public Elephant(string _majorClass):base(_majorClass)
   {
   }


  public void Ride()

  {

   Console.WriteLine("Elephant can ride 34KPM");

  }

}

class Horse:Animal,IRide

{
   public Horse(){}

   public Horse(string _majorClass):base(_majorClass)
   {

   }



    public void Ride()

    {
      Console.WriteLine("Horse can ride 110 KPH");
    }

}


class Test

{
    static void Main()

    {
       Elephant bully = new Elephant("Vertebrata");
       Horse    lina  = new Horse("Vertebrata");
       IRide[] riders = {bully,lina};

       foreach(IRide rider in riders)
       {

         rider.Ride();
       }

               Console.ReadKey(true);
    }
} 

}

Questions:

Besides such an extension, how can we use the elegance of interfaces?

What is the key point that I can say, this can only be done using the interface (except

multiple inheritance)?

(I want to collect information from experienced hands).

Edit:

Edited as conceptual, I think.

+3
source share
7 answers

# ( IMHO), , .

interface IRideable
{
    void Ride();
}

class Elephant : Animal, IRideable{}

class Unicycle: Machine, IRideable{}

, , , ( , ) , , - .

public static void RideThemAll(IEnumerable<IRideable> thingsToRide)
{
  foreach(IRideable rideable in thingsToRide)
     ridable.Ride();
}
+5

, Bike, IRide, Animal. , , , .

+6

, , IRide Animal IRide[].

, IRide - . , Ride(), Eat() ""?

, , . "" ( - ).

+3

. .

+1

. , . :

public void Feed(IRide rideable)
{
    //DO SOMETHING IMPORTANT HERE

    //THEN DO SOMETHING SPECIFIC TO AN IRide object
    rideable.Eat();
}

, Feed, IRide, . , . . Inversion of Control, Structure Map , Rhino Mock.

+1

"" , . (Introspection Reflection) , .

.NET(, ISerializable) DI.

0

, , .

- , : " !" , .

, : " , !"

Of course, a class can implement as many interfaces as it wants, but only one base class can inherit.

0
source

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


All Articles