简体   繁体   English

如何让一个 function 创建多个不同类型的相同 class

[英]How to have one function create multiple different types of the same class

I want to make a deck of 16 cards, with a number of each type of card in the deck.我想制作一副 16 张牌,每副牌都有一些牌。

This is my current solution but it does not look good.这是我目前的解决方案,但看起来不太好。

How do I make it more clean and make it follow OOP Principles.如何让它更干净,并使其遵循 OOP 原则。

Each of the pieces(guards,priests,etc) inherits from the Card Class.每个部分(守卫、牧师等)都继承自卡 Class。

Is Generic types something I should be looking at here?我应该在这里查看通用类型吗?

Code image with formatting带格式的代码图像

 class Deck
{
    public List<Card> Cards;
    int NumberofGuards = 5;
    int NumberofPriests = 2;
    int NumberofBarons = 2;
    int NumberofMaids = 2;
    int Numberofprinces = 2;
    int NumberofKings = 1;
    int NumberofCountesses = 1;
    int NumberofPrincesses = 1;
    public Deck()
    {
        //Guards
        for (int i = 0; i < NumberofGuards; i++)
        {
            Cards.Add(new Guard());
        }
        //Priests
        for (int i = 0; i < NumberofPriests; i++)
        {
            Cards.Add(new Priest());
        }
        //Barons
        for (int i = 0; i < NumberofBarons; i++)
        {
            Cards.Add(new Baron());
        }
        //maids
        for (int i = 0; i < NumberofMaids; i++)
        {
            Cards.Add(new Maid());
        }
        }
    }

}

abstract class Card
{
    public string Name { get; set; }
    public int Number { get; set; }
    public abstract void Keep();
    public abstract void Discard();
    public abstract void Use();
}

This would be a good use for a factory pattern implementation.这将是工厂模式实现的一个很好的用途。

public class CardFactory<T> where T : Card, new()
{
    public static List<T> BuildMany(int count) {
        var list = new List<T>();
        for (int x=0; x<count; x++)
        {
            list.Add(new T());
        }
        return list;
    }
}

Then, modify your Deck class to use it:然后,修改您的Deck class 以使用它:

public class Deck公共 class 甲板

{
    public readonly List<Card> Cards;
        
    public Deck(int numberOfGuards) // [numberOfPriests, etc]
    {
        // Guards
        Cards.AddRange(CardFactory<Guard>.BuildMany(numberOfGuards));

        // [...]
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何反序列化包含具有相同父类的不同类类型的对象的 JSON 数组 - How to de-serialize JSON array containing objects of different class types that have same parent class 如果对象具有相同的基数 class,如何处理具有不同返回类型的方法? - How to handle methods with different return types if the objects have same base class? 相同的代码针对不同的数据类型重复多次; 有没有办法使一个函数可以处理所有可能的类型? - Exact same code repeated multiple times for different data types; Any way to make one function that can handle all possible types? 如何创建不同类型的多个配置项 - How to create multiple configuration items of different types 当两个对象都是不同的类型但是从同一个父类继承时,如何将一个对象转换为另一个对象 - How to convert one object to another when both are different types but inherit from the same parent class 注册不同类型的同一个班级 - Register same class for different types 在不同的类类型上创建方法 - Create method on different class types 如何创建类的(多态)集合/列表/数组,其中每个类可能具有一些不同的属性 - How to create a (Polymorphism) collection/list/array of a class where each one may have some different properties C#如何在一个函数中创建一个类的实例,然后在另一个函数中使用同一个实例 - C# how to create an instance of a class in one function and then use that same instance in another function 是否可以创建一个字典,其中的值可以具有多种数据类型? (另外我如何遍历一个数组作为我的值之一) - Is it possible to create a dictionary in which values can have multiple data types? (Also how do I iterate through an array as one of my values)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM