博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1播放器练习讲解

Posted on 2010-10-12 22:30  EVON168  阅读(116)  评论(0编辑  收藏  举报
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PlayerTask
{
    
/// <summary>
    
/// 播放器类型
    
/// </summary>
    public enum PlayerType
    { 
        MP3,
        WalkMan,
        iPod
    }

    
public abstract class Player
    {
        
private int _Price;

        
public int Price
        {
            
get { return _Price; }
        }

        
private string _Color;

        
public string Color
        {
            
get { return _Color; }
        }

        
public Player(int price):this(price,"无色")
        {

        }

        
public Player(int price,string color)
        {
            
this._Price = price;
            
this._Color = color;
        }

        
public void Show()//显示价格{0},颜色{1}
        {
            Console.WriteLine(
"价格{0},颜色{1}",this._Price,this._Color);
        }

        
public abstract void Play();//抽象方法
    }

    
public class Mp3 : Player
    {
        
public Mp3():base(150)
        {

        }
        
public override void Play()
        {
            Console.WriteLine(
"MP3播放");
        }
    }

    
public class WalkMan : Player
    {
        
public WalkMan():base(200)
        {

        }

        
public override void Play()
        {
            Console.WriteLine(
"WalkMan播放");
        }
    }


    
public class iPod : Player
    {
        
public iPod():base(300)
        {

        }

        
public override void Play()
        {
            Console.WriteLine(
"iPod播放");
        }
    }


    
public class Young
    {
        
public void ListenMusic(Player player)
        {
            player.Play();
        }
    }

    
public class Shop
    {
        
public Player Sale(PlayerType type)
        {
            Player p 
= null;
            
if (type == PlayerType.MP3)
            {
                p 
= new Mp3();
            }
            
else if (type == PlayerType.iPod)
            {
                p 
= new iPod();
            }
            
else
            {
                p 
= new WalkMan();
            }
            
return p;
        }


        
public Player Sale(int price)
        { 
            Player p
=null;
            
if (price > 1000)
            {
                p 
= new iPod();
            }
            
else if (price >= 500)
            {
                p 
= new Mp3();
            }
            
else
            {
                p 
= new WalkMan();
            }
            
return p;
        }
    }

    
class Program
    {
        
static void Main(string[] args)
        {
            
//听音乐
            Player p = null;
            
//让mp3播放
            p = new Mp3();
            Young y 
= new Young();
            y.ListenMusic(p);

            
//让Ipod播放
            p = new iPod();
            y.ListenMusic(p);

            
//购买
            Shop s = new Shop();
            p 
= s.Sale(1200);
            p.Play();

            p 
= s.Sale(PlayerType.MP3);
            p.Play();

        }
    }
}