小编Dav*_*man的帖子

从2个派生类中选择正确的枚举列表

我正在建立一个图书馆应用程序,我有一本抽象的书籍类,两种类型的衍生书籍和两本能够保存书籍类型的枚举.每本书都可以与一种或多种类型相关.

    abstract public class Book
    {   
       public int Price { get; set; } 
       ...
    }

    public enum ReadingBooksGenre
    {
       Fiction,
       NonFiction
    }

    public enum TextBooksGenre
    {
        Math,
        Science
    } 

    abstract public class ReadingBook : Book
    { 
        public List<ReadingBooksGenre> Genres { get; set; }
    }

    abstract public class TextBook : Book
    {   
        public List<TextBooksGenre> Genres { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

现在我想根据书籍类型保存折扣(没有双倍折扣,只计算最高折扣),所以我正在考虑制作两个字典,以保存每种类型的所有折扣,如​​下所示:

    Dictionary<ReadingBooksGenre, int> _readingBooksDiscounts;
    Dictionary<TextBooksGenre, int> _textBooksDiscounts;
Run Code Online (Sandbox Code Playgroud)

所以现在我需要检查每本书的类型,以便找到最高的折扣,有没有更好的方法来做到:

    private int GetDiscount(Book b)
    {
        int maxDiscount = 0;
        if (b is ReadingBook) …
Run Code Online (Sandbox Code Playgroud)

c# enums modeling dictionary

5
推荐指数
1
解决办法
216
查看次数

标签 统计

c# ×1

dictionary ×1

enums ×1

modeling ×1