---
# System prepended metadata

title: mod10
tags: [C#]

---

```csharp=
// abstract class
abstract class Shape
{
    public string Id { get; set; }
    public abstract double Area { get; }
    public virtual void ShowInfo() =>
        Console.WriteLine($"Id:{Id}, Area:{Area}");
}

class Rectangle : Shape
{
    public double Width { get; set; }
    public double Height { get; set; }

    public override double Area => Width*Height;
}
class Circle:Shape
{
    public double Radius { get; set; }

    public override double Area => Radius*Radius*Math.PI;
}

Rectangle rectangle = new Rectangle
{
    Id = "矩形1",
    Width = 20,
    Height = 10
};
Circle circle = new Circle
{
    Id = "圓形1",
    Radius = 10
};

Shape[] shapes = { rectangle, circle };

foreach (var item in shapes)
{
    item.ShowInfo();
}
//==================================
//interface
interface IPlayable
{
    void Play();
    void Pause();
}
class DVDPlayer : IPlayable
{
    public void Pause()
    {
        Console.WriteLine((nameof(DVDPlayer), nameof(Pause)));
    }
    public void Play()
    {
        Console.WriteLine((nameof(DVDPlayer), nameof(Play)));
    }

}
DVDPlayer player1= new DVDPlayer();
player1.Play();
player1.Pause();
//=============================
interface IPlayable2
{
    void Stop();
}
 class DVDPlayer : IPlayable, IPlayable2
 {
     public void Pause()
     {
         Console.WriteLine((nameof(DVDPlayer), nameof(Pause)));
     }
     public void Play()
     {
         Console.WriteLine((nameof(DVDPlayer), nameof(Play)));
     }

     public void Stop()
     {
         Console.WriteLine((nameof(DVDPlayer), nameof(Stop)));
     }
 }

DVDPlayer player1= new DVDPlayer();

CDPlayer player2 = new CDPlayer();

IPlayable[] players = {player1,player2};
foreach (var item in players)
{
    item.Play();
    item.Pause();
    if(item is IPlayable2 x)
    {
        x.Stop();
    }
}

```