⏱️ 1 שעה | 🎯 ממשקים והתנהגויות משותפות
using System;
// Interface - חוזה של פעולות
interface IPlayable
{
void Play();
void Pause();
void Stop();
}
// Song מממש את IPlayable
class Song : IPlayable
{
public string Title;
public string Artist;
public Song(string title, string artist)
{
Title = title;
Artist = artist;
}
public void Play()
{
Console.WriteLine($"🎵 מנגן: {Title} - {Artist}");
}
public void Pause()
{
Console.WriteLine("⏸️ השיר מושהה");
}
public void Stop()
{
Console.WriteLine("⏹️ השיר נעצר");
}
}
// Video גם מממש את IPlayable
class Video : IPlayable
{
public string Title;
public int Duration;
public Video(string title, int duration)
{
Title = title;
Duration = duration;
}
public void Play()
{
Console.WriteLine($"▶️ מפעיל: {Title} ({Duration} דקות)");
}
public void Pause()
{
Console.WriteLine("⏸️ הוידאו מושהה");
}
public void Stop()
{
Console.WriteLine("⏹️ הוידאו נעצר");
}
}
class Program
{
static void Main()
{
// רשימה של IPlayable - יכולה להכיל Song ו-Video!
List<IPlayable> playlist = new List<IPlayable>();
playlist.Add(new Song("Imagine", "John Lennon"));
playlist.Add(new Video("Learn C#", 45));
playlist.Add(new Song("Bohemian Rhapsody", "Queen"));
// מפעילים הכל באותה צורה!
foreach (IPlayable item in playlist)
{
item.Play();
}
}
}
צרו ממשק IDrawable עם Method Draw()
מחלקות: Circle, Square, Triangle
כל אחת מדפיסה משהו שונה ב-Draw
ממשק ICalculator עם Calculate(int a, int b)
מחלקות: AddCalculator, MultiplyCalculator
List<ICalculator> + קריאה לכולם