C# 面向对象(Mp3播放器案例)

来源:互联网 发布:多伦多大学士嘉堡 知乎 编辑:程序博客网 时间:2024/06/06 09:39
实现Mp3播放器简单功能:

对象:播放器: 行为:1.添加歌曲,2播放歌曲,3上一首/下一首,4.停止播放;
对象:歌曲信息:属性:1.标题,2.歌手,3.时长,,,

    class Program
    {
       static void Main(string[] args)
       {
           Song s1 =new Song();
           s1.title ="灰色头像";
           s1.singer= "许嵩";
           s1.lenght= 500;

           Song s2 =new Song();
           s2.title ="告白气球";
           s2.singer= "周董";
           s2.lenght= 552;

           Mp3 m =new Mp3();
           m.songs =new Song[10];
          m.Add(s1);
          m.Add(s2);

          m.play();
          m.tiack();
          m.next();
          m.stop();

          Console.ReadLine();
       }
    }

    class Song
    {
       public string singer { set; get; }
       public string title { set; get; }
       public int lenght { set; get; }

       public override string ToString()
       {
           returnsinger+"-"+title+"("+lenght+")";
       }
    }

    class Mp3
    {
       public Song[] songs { set; get; }

       private int index = 0;
       ///
       /// 添加歌曲
       ///
       /// 歌曲名称
       public void Add(Song song)
       {
           if (index> songs.Length - 1)
           {
             Console.WriteLine("添加歌曲已满:");
              return;
           }
          songs[index] = song;
          index++;
          Console.WriteLine("歌曲已经添加到列表:"+song);
       }
       
       ///
       /// 播放第一首歌曲
       ///
       public void play()
       {
           if (index> 0)
           {
              Console.WriteLine("正在播放:" +songs[0]);
           }
           else
           {
             Console.WriteLine("当前列表中没有歌曲播放");
           }
       }

       private int curindex = 0;
       ///
       /// 上一首
       ///
       public void tiack()
       {
           if (index< 0)
           {
             Console.WriteLine("当前列表中没有歌曲播放");
              return;
           }
           curindex =(curindex-1 < 0 ? index-1 : curindex - 1);
          Console.WriteLine("正在播放:"+songs[curindex]);
       }
    

    ///
    /// 下一首
    ///
           publicvoid next()
           {
              if (index < 0)
              {
                 Console.WriteLine("当前列表中没有歌曲播放");
                 return;
              }
              curindex = (curindex + 1 >index - 1 ? 0 : curindex + 1);
              Console.WriteLine("正在播放:" +songs[curindex]);
           }

       public void stop()
       {
          Console.WriteLine("停止播放"+songs[curindex]);
       }
    }


原创粉丝点击