开发者

c# preventing inheritance of a method

开发者 https://www.devze.com 2023-02-12 02:34 出处:网络
For a poker parser (my first project for learning c#), I have a parent class clCardsHeld and two child classes clHiHand and clLoHand. clCardsHeld also contains properties and private fields, of each o

For a poker parser (my first project for learning c#), I have a parent class clCardsHeld and two child classes clHiHand and clLoHand. clCardsHeld also contains properties and private fields, of each of the two child types. 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). (HiHand shouldn't have another HiHand as a property of itself, nor开发者_C百科 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.)

I made the base class fields private, instead of protected, so the inherited property remains null, if I've discerned that correctly. 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.

Thanks for any help.


As mentioned in the link referenced by Adeel, the inheritance in this situation is unnecessary. Your high hand and low hands should just be new instances of 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.


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.
  • 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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号