从左往右,btnMP3,lblShow,btnAVI
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
IPlayer iplayer;
MP3 mp3;
AVI avi;
interface IPlayer //接口定义
{
string Play(); //播放
string Stop(); //停止
string Pause(); //暂停
string Pre(); //上一首
string Next(); //下一首
}
public class MP3 : IPlayer
{
public string Play()
{
return "正在播放MP3歌曲!";
}
public string Stop()
{
return "停止播放MP3歌曲!";
}
public string Pause()
{
return "暂停播放MP3歌曲!";
}
public string Pre()
{
return "播放上一首MP3歌曲!";
}
public string Next()
{
return "播放下一首MP3歌曲!";
}
}
public class AVI : IPlayer
{
public string Play()
{
return "正在播放AVI视频!";
}
public string Stop()
{
return "停止播放AVI视频!";
}
public string Pause()
{
return "暂停播放AVI视频!";
}
public string Pre()
{
return "播放上一首AVI视频!";
}
public string Next()
{
return "播放下一首AVI视频!";
}
}
private void btnAVI_Click(object sender, EventArgs e)
{
avi = new AVI(); //新建AVI对象
iplayer = (IPlayer)avi; //将avi转化为接口
}
private void btnMP3_Click(object sender, EventArgs e)
{
mp3 = new MP3(); //新建Mp3对象
iplayer = (IPlayer)mp3; //将MP3转换为接口
}
private void button3_Click(object sender, EventArgs e)//上一首
{
if (mp3 is IPlayer || avi is IPlayer) //判断
{
lblShow.Text = "\n" + iplayer.Pre();
}
else
{
MessageBox.Show("请选择播放MP3歌曲或AVI视频!");
}
}
private void button5_Click(object sender, EventArgs e)//停止
{
if (mp3 is IPlayer || avi is IPlayer) //判断类型
{
lblShow.Text = "\n" + iplayer.Stop();
}
else
{
MessageBox.Show("请选择播放MP3歌曲或AVI视频!");//判断是否为空
}
}
private void button6_Click(object sender, EventArgs e)//播放
{
if (mp3 is IPlayer || avi is IPlayer)
{
lblShow.Text = "\n" + iplayer.Play();
}
else
{
MessageBox.Show("请选择播放MP3歌曲或AVI视频!");
}
}
private void button7_Click(object sender, EventArgs e)//暂停
{
if (mp3 is IPlayer || avi is IPlayer)
{
lblShow.Text = "\n" + iplayer.Pause();
}
else
{
MessageBox.Show("请选择播放MP3歌曲或AVI视频!");
}
}
private void button4_Click(object sender, EventArgs e)//下一首
{
if (mp3 is IPlayer || avi is IPlayer)
{
lblShow.Text = "\n" + iplayer.Next();
}
else
{
MessageBox.Show("请选择播放MP3歌曲或AVI视频!");
}
}
}
}