Cześć, dobrze to rozumiem? Interfejs pozwala nam zrobić kolekcję elementów różnych klas (które implementują ten interfejs). A co co poza tym, bo jakoś mam problem ze zrozumieniem tego zagadnienia
najlepiej jakiś krótki/realny przykład, nie będący typu foo/bar aaa/bbb kotek/pies :P
public interface IFlyable
{
void Fly();
}
class Bird : IFlyable
{
public void Fly()
{
Console.WriteLine("bird flies");
}
}
class Plane : IFlyable
{
public void Fly()
{
Console.WriteLine("plane flies");
}
}
public class Program
{
public static List<IFlyable> GetBirdInstancesAndPlaneInstancesMixed()
{
return new List<IFlyable> {new Bird(), new Plane()};
}
public static void Main(string[] args)
{
List<IFlyable> things = GetBirdInstancesAndPlaneInstancesMixed();
foreach(IFlyable item in things)
{
item.Fly();
}
}
}