简体   繁体   English

c#阻止了方法的继承

[英]c# preventing inheritance of a method

For a poker parser (my first project for learning c#), I have a parent class clCardsHeld and two child classes clHiHand and clLoHand . 对于扑克解析器(我的第一个学习c#的项目),我有一个父类clCardsHeld和两个子类clHiHandclLoHand clCardsHeld also contains properties and private fields, of each of the two child types. clCardsHeld还包含两种子类型中的每一种的属性和私有字段。 And so what I end up with, with this inheritance, is, eg: 所以我最终得到的是这种继承,例如:

public class clCardsHeld
{public clHiHand HiHand { Get {} Set {} }
} 
public class clHiHand : clCardsHeld{}

clCardsHeld Hand3 = new clCardsHeld(); 
clHiHand hi = new clHiHand(); 
hi = Hand3.HiHand.HiHand.HiHand; 
hi = Hand3.HiHand.LoHand.HiHand;

(as prompted by intellisense). (由intellisense提示)。 ( HiHand shouldn't have another HiHand as a property of itself, nor a low hand. But CardsHeld has a HiHand and LoHand as properties in, eg, games where you get 7 cards and you can use 5 to make to best possible hand and 5 for the worst possible, both played at the same time, for half the pot each.) HiHand不应该有另一个HiHand作为自己的属性,也不应该是低手。但是CardsHeld有一个HiHandLoHand作为属性,例如,你获得7张牌的游戏,你可以使用5来制作最好的牌和5为最坏的可能,两者同时播放,每个锅的一半。)

I made the base class fields private, instead of protected, so the inherited property remains null , if I've discerned that correctly. 我将基类字段设为私有,而不是保护,因此如果我已正确识别,则继承的属性保持为null But really I'd like the property not to be inherited. 但我真的希望这个财产不被继承。 Trying to research this, I see that a property can only be sealed when it overwrites a base property. 试图研究这个,我看到只有在覆盖基本属性时才能密封属性。 And "hiding" the method generally refers to having different code in the child... . 并且“隐藏”该方法通常是指在孩子中具有不同的代码....

One other thing, while I'm throwing out my novice questions: 另外一件事,当我抛弃我的新手问题时:

    public class clCardsHeld { private List<clCards> _liCards; }
public class clHiHand : clCardsHeld 

I can find clCardsHeld._liCards in the locals window, but I can't find clCardsHeld.clHiCards._liCards in the locals window (but I can get at it by assigning it to a variable. 我可以在locals窗口找到clCardsHeld._liCards ,但我在locals窗口找不到clCardsHeld.clHiCards._liCards (但是我可以通过将它分配给变量来获取它。

Thanks for any help. 谢谢你的帮助。

As mentioned in the link referenced by Adeel, the inheritance in this situation is unnecessary. 正如Adeel引用的链接中所提到的,这种情况下的继承是不必要的。 Your high hand and low hands should just be new instances of clCardsHeld: 你的高手和低手应该只是clCardsHeld的新实例:

public class clCardsHeld
{
    private List<clCards> _liCards;

    public clCardsHeld GetLoHand()
    {
        //Logic here for returning the low hand, using _liCards
    }

    public clCardsHeld GetHiHand()
    {
        //Logic here for returning the high hand, using _liCards
    }
}

The GetLoHand() and GetHiHand() functions will simply return a different set of held cards. GetLoHand()和GetHiHand()函数将只返回一组不同的持卡。

There are a couple of misconceptions in your post.. 您的帖子中有一些误解。

  • If you want a property not to be inherited, you are violating the Liskov Substitution Principle . 如果您想要一个不被继承的财产,那么您违反了Liskov替代原则
  • Fields are not null in a subclass just because they are private to the base class. 字段在子类中不为空,因为它们对基类是私有的。 They can still be assigned by the logic in the base class. 它们仍然可以由基类中的逻辑分配。
  • It sounds to me like you are using inheritance when you don't need to.. Use inheritance when a concept (subclass) is a special case of another concept (base class). 听起来像你在不需要时使用继承。当一个概念(子类)是另一个概念(基类)的特例时,使用继承。 And favor composition over inheritance. 并且赞成合成而不是继承。

I think you should split what is common to all the objects into a separate base class: 我认为你应该将所有对象的共同点分成一个单独的基类:

class HandBase { /* common to both HiHand an LoHand */}
class LoHand : HandBase {}
class HiHand : HandBase {}

From what I can tell from your explanation, cCardsHeld does not have to have the same base type: 从我的解释中可以看出,cCardsHeld不必具有相同的基类型:

class CardsHeld
{
    public LoHand LoHand { get; private set;}
    public HiHand HiHand { get; private set;}
}

But if it does, then you could do this: 但如果确实如此,那么你可以这样做:

class CardsHeld : HandBase
{
    public LoHand LoHand { get; private set;}
    public HiHand HiHand { get; private set;}
}

If there is a property that you don't want subclasses to have, then don't put it in the base class. 如果存在您不希望子类具有的属性,则不要将其放在基类中。

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

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