简体   繁体   English

有关静态类和继承的C#设计

[英]C# Design Regarding Static Classes and Inheritence

I am designing a card game in C#. 我正在用C#设计纸牌游戏。 Each card is an instance of the Card class that contains two properties: a unique ID for the card and a reference to a CardData object. 每个卡都是Card类的实例,该实例包含两个属性:卡的唯一ID和对CardData对象的引用。 The object of type CardData implements the methods regarding how the Card functions within the game rules. CardData类型的对象实现有关Card如何在游戏规则中起作用的方法。 Many cards (ex. if there are multiple jack of clubs) can reference the same card data. 许多卡(例如,如果有多个球杆插孔)可以引用相同的卡数据。

public class Card {
    public int id;
    public CardData card;
}

My question is regarding how I should implement the CardData class. 我的问题是关于我应该如何实现CardData类。 My first instinct was to design an abstract base class CardData with a bunch of methods like WhenPlayed() and WhenRemoved() , and then have a subclass for each card that implements them. 我的第一个直觉是用一堆诸如WhenPlayed()WhenRemoved()类的方法设计一个抽象基类CardData ,然后为实现它们的每个卡都有一个子类。

public abstract class CardData () {
    public abstract void WhenPlayed();
    public abstract void WhenRemoved();
}

public class JackOfClubs : CardData () {
    public override void WhenPlayed()  { 
        Console.WriteLine( "You played a Jack of Clubs!" );
    }
    public override void WhenRemoved() {
        Console.WriteLine( "Bye for now" );
    }
}

However, since I never want to actually instantiate a JackOfClubs object, but rather instantiate Card s and give them a reference to the JackOfClubs class, it would make sense to make the CardData class and all subclasses static . 但是,由于我从不想实际实例化JackOfClubs对象,而是实例化Card并给它们提供对JackOfClubs类的引用,因此使CardData类和所有子类为staticCardData This creates a problem, because as far as I know static classes cannot inherit. 这造成了一个问题,因为据我所知静态类不能继承。

Furthermore, I considered using an interface but I wanted the possibility of default methods. 此外,我考虑使用interface但我希望使用默认方法。 For instance, if 90% of my CardData objects implement WhenRemoved() as "Bye for now", it would be tiresome to retype that for each subclass. 例如,如果我90%的CardData对象将WhenRemoved()实现为“ Bye now”,那么为每个子类重新键入该内容将很麻烦。 I could use a non abstract CardData class to achieve this, but not an interface. 我可以使用非抽象CardData类来实现此目的,但不能使用接口。

My question is what would be the best or correct approach to handling a class structure such as this, if there is one. 我的问题是,如果有的话,处理这样的类结构的最佳或正确方法是什么?

I would do a design like this please refer the diagram , so that you don't create as many classes as there are many cards, essentially they are different objects of the same class. 我会做这样的设计, 请参考图 ,这样就不会创建太多的类,因为它们实际上是同一类的不同对象。 So we have factory that holds a custom collection called Cards which is a collection of Card class that has a type and value. 因此,我们有一个工厂,拥有一个名为Cards的自定义集合,该集合是具有类型和值的Card类的集合。 You can implement the play method depending on the type and value o the card. 您可以根据卡的类型和值来实现播放方法。 In the case that you are playing a game where all the cards are not involved, this design just works fine, in the case that you are extending this to multiple card games, if you add one more class card category, this design encompasses that as well. 如果您正在玩的游戏中并没有涉及所有纸牌,那么这种设计就可以很好地工作;如果要将其扩展到多个纸牌游戏中,那么如果再添加一个类别的纸牌类别,则该设计将好。 Hope this helps. 希望这可以帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM