C#中用接口实现鸭子案例

描述:橡皮rubber鸭子、木wood鸭子、真实的鸭子realduck。三个鸭子都会游泳,而橡皮鸭子和真实的鸭子都会叫,只是叫声不一样,橡皮鸭子“唧唧”叫,真实地鸭子“嘎嘎”叫,木鸭子不会叫。

分析:定义两个接口,三个子类鸭子,工厂中有两个方法,主程序分别去调用两个工厂中的方法。

具体实现如下:

//游泳接口
    interface ISwimable
    {
        void Swimming();
    }

//叫接口
    interface IShoutable
    {
        void Shouting();
    }

//真实的鸭子
    class RealDuck:ISwimable,IShoutable
    {
        public RealDuck()
        { }

        public void Swimming()
        {
            Console.WriteLine("真实的鸭子能够在水中快速的游玩!");
        }

        public void Shouting()
        {
            Console.WriteLine("真实的鸭子叫声是:嘎嘎嘎嘎嘎嘎……");
        }
    }

//橡皮鸭子
    class RubberDuck:ISwimable,IShoutable
    {
        public RubberDuck()
        { }

        public void Swimming()
        {
            Console.WriteLine("橡皮的鸭子能够在水中慢慢的游!");
        }

        public void Shouting()
        {
            Console.WriteLine("橡皮的鸭子叫声是:唧唧唧唧唧唧……");
        }
    }

 //木头鸭子
    class WoodDuck:ISwimable
    {
        public WoodDuck()
        { }
   
        public void Swimming()
        {
            Console.WriteLine("木头的鸭子能够在水中轻轻的游!");
        }
    }

//简单工厂模式

class Factory
    {
        //游泳
        public static ISwimable CreateSwiming(string name)
        {
            switch (name)
            {
                case "1": return new RealDuck();
                case "2": return new RubberDuck();
                case "3": return new WoodDuck();
                default:
                    throw new Exception("系统找不到您选择的鸭子!");
            }
 
        }

        //叫
        public static IShoutable CreateShouting(string name)
        {
            switch (name)
            {
                case "1": return new RealDuck();
                case "2": return new RubberDuck();
                default:
                    throw new Exception("系统找不到您选择的鸭子!");
            }
        }
    }



//控制台主程序

static void Main(string[] args)
        {
            //用户输入
            while (true)
            {
                Console.WriteLine("请选择您要查看的鸭子,如下:");
                Console.WriteLine("1、真实鸭子 2、橡皮鸭子 3、木头鸭子");
                string name = Console.ReadLine();
               
                if (name=="1" ||name=="2"||name=="3")
                {
                    if (name != "3")
                    {
                        //叫
                        IShoutable shout=  Factory.CreateShouting(name);
                        shout.Shouting();
                    }
                    //游泳
                    ISwimable swim = Factory.CreateSwiming(name);
                    swim.Swimming();
                    Console.WriteLine();
                    continue;
                }
                Console.WriteLine("系统找不到您选择的鸭子……");                
            }           
        }

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值