A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 海枫 中级黑马   /  2014-5-20 17:56  /  1628 人查看  /  4 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 海枫 于 2014-5-20 21:45 编辑

28、超市现在提供3种打折方式,1、打95折,2、打8折,3、买500送100。
要求:写一个打折的抽象父类,并且在父类中提供一个打折的抽象方法,
子类继承父类后,重写父类总的打折抽象方法,在控制台中提示用户请选择一个打折方式,根据用户的输入,
使用简单工厂设计模式获得一个打折的对象,并计算打折后的价钱。(价钱可以自己定义)

怎样使用简单工厂模式过的一个打折对象?

评分

参与人数 1技术分 +1 收起 理由
czwanglei + 1

查看全部评分

4 个回复

倒序浏览
我理解的意思是这样的..
1.先写一个抽象类,里面提供一个抽象方法比如:public abstract double DiscountWay(double Price);

2.然后写3个子类,每个类都继承Discount类并实现抽象方法;但是实现是以不同的3种打折方式实现.

3.写一个静态工厂类:DiscountFactory,里面定义一个public static  Discount getDiscount(int i)方法,方法里根据
i的值创建3个不同子类的对象(比如i=1就创建第一种打折方式的子类对象,i==2就创建第二种)
4.在主程序里通过判断获得用户输入的值,通过DiscontFactory返回对象,获得打折后的价钱并返回.

这道题实际上考的就是多态,把子类对象赋给父类变量.希望能够帮到你:)

评分

参与人数 1技术分 +1 收起 理由
czwanglei + 1

查看全部评分

回复 使用道具 举报
哥们,我按照你的要求做了一下,看看符不符合你的要求;
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;

  6. namespace 简单的工厂模式_超市打折
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Console.WriteLine("超市现在提供3种打折方式,1、打95折,2、打8折,3、买500送100。");
  13.             Console.WriteLine("请选择:1还是2还是3?");
  14.             string str = Console.ReadLine();
  15.             DisCount Discount = null;
  16.             DiscountFactory Mydis = new DiscountFactory();

  17.             switch (str)
  18.             {
  19.                 case "1":
  20.                     Discount = Mydis.MakeOne();
  21.                     Discount.Discount();
  22.                     break;
  23.                 case "2":
  24.                     Discount = Mydis.MakeTwo();
  25.                     Discount.Discount();
  26.                     break;
  27.                 case "3":
  28.                     Discount = Mydis.MakeThree();
  29.                     Discount.Discount();
  30.                     break;
  31.                 default:
  32.                     break;
  33.             }
  34.             Console.ReadKey();
  35.         }
  36.     }

  37.     public abstract class DisCount//打折父类
  38.     {
  39.         public DisCount() { }
  40.         public abstract void Discount();
  41.     }

  42.     public class OneDiscount : DisCount
  43.     {
  44.         public override void Discount()
  45.         {
  46.             Console.WriteLine( "打95折");
  47.         }

  48.     }
  49.     public class TwoDiscount : DisCount
  50.     {
  51.         public override void Discount()
  52.         {
  53.             Console.WriteLine("打8折");
  54.         }
  55.     }
  56.     public class ThreeDiscount : DisCount
  57.     {
  58.         public override void Discount()
  59.         {
  60.             Console.WriteLine("买500送100");
  61.         }
  62.     }

  63.     public class DiscountFactory
  64.     {
  65.         public OneDiscount MakeOne()
  66.         {
  67.             return new OneDiscount();
  68.         }
  69.         public TwoDiscount MakeTwo()
  70.         {
  71.             return new TwoDiscount();
  72.         }
  73.         public ThreeDiscount MakeThree()
  74.         {
  75.             return new ThreeDiscount();
  76.         }
  77.     }
  78. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
czwanglei + 1

查看全部评分

回复 使用道具 举报
Mekor 发表于 2014-5-21 11:28
哥们,我按照你的要求做了一下,看看符不符合你的要求;

very thank you!
回复 使用道具 举报
二楼狂赞啊
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马