我可以将某个枚举的枚举限制在另一个枚举的某些情况下吗? [英] Can I restrict an enum to certain cases of another enum?

查看:94
本文介绍了我可以将某个枚举的枚举限制在另一个枚举的某些情况下吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个面包店和一个原料清单:

 枚举成分{
case flower = 1
case sugar = 2
case yeast = 3
case eggs = 4
case milk = 5
case almonds = 6
case chocolate = 7
case salt = 8
}

案例 rawValue 表示库存号码。



然后我有两个食谱:



巧克力蛋糕: / h3>


  • 500g花

  • 300g糖


  • 200ml牛奶

  • 200g巧克力



杏仁蛋糕:




  • 300g花

  • 200g糖

  • 20g酵母

  • 200g杏仁

  • 5个鸡蛋

  • 2g盐



    • 现在我定义一个函数

        func bake(含成分:[成分]) - >蛋糕

      当然,我相信我的员工,但我仍然想确保他们只使用正确的成分烤蛋糕

      Say I have a bakery and an inventory of ingredients:

      enum Ingredient {
          case flower     = 1
          case sugar      = 2
          case yeast      = 3
          case eggs       = 4
          case milk       = 5
          case almonds    = 6
          case chocolate  = 7
          case salt       = 8
      }
      

      A case's rawValue represents the inventory number.

      Then I have two recipes:

      Chocolate Cake:

      • 500g flower
      • 300g sugar
      • 3 eggs
      • 200ml milk
      • 200g chocolate

      Almond Cake:

      • 300g flower
      • 200g sugar
      • 20g yeast
      • 200g almonds
      • 5 eggs
      • 2g salt

      Now I define a function

      func bake(with ingredients: [Ingredient]) -> Cake
      

      Of course I trust my employees, but I still want to make sure they only use the right ingredients to bake a cake.

      这篇关于我可以将某个枚举的枚举限制在另一个枚举的某些情况下吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆