简体   繁体   中英

Access modifiers C#

I have a class Card and it has some attributes that get their value from an external API:

public bool IsCard { get { return this._isCard; } }
public bool IsSpell { get { return this._isSpell; } }
public bool IsMinion { get { return this._isMinion; } }
public bool IsWeapon { get { return this._isWeapon; } }
public bool IsPower { get { return this._isPower; } }
public bool IsPlayer { get { return this._isPlayer; } }
public bool IsGame { get { return this._isGame; } }
public bool IsHero { get { return this._isHero; } }

So this would be like my master class for all the cards, including Weapons, Spells and Minions. (Of course the real class has way more members.)

Then I have a Minion class which inherits from Card ( Minion : Card )

The problem I face is that obviously I can access those members from a Minion (because they're public), and that doesn't even make sense ( Minion.IsPlayer ? clearly not) so my question is: How can I keep those members only for the Card class and not for its children?

The private modifier won't grant me access later in other part of the library so I can't use it.

The problem i see with your design isn't the access modifiers, its just simply incorrect.

It doesn't make a whole lot of sense to have a Card object with a IsCard property, that is also why IsMinion property on a Minion object doesn't make sense either. Those properties don't really belong to a Card object.

I suggest rethinking your design. It looks as if this Card class is an enum containing all those player types:

public enum Card
{
   Minion,
   Weapon,
   Spell,
   // etc..
 }

When something seems peculiar in your inheritance hierarchy such as a public property not belonging in a derived class, its usually a sign to stop and think about how you planned things out and why they dont seem right. In OOP, its alot about creating the right models to fit the problem.

Some things to note:

Inheritance is an "is-a" relationship. Minion : Card means "a Minion is a Card ". This may or may not be your intent.

You can already ask whether an object is a Card or a Minion or a Spell using the language. There are a few ways to do this:

Using the is keyword: ( link )

if (myObject is Spell)

Comparing Type s: ( Type typeof )

if (myObject.GetType() == typeof(Spell))

Using the IsAssignableFrom method ( link ):

if (typeof(Spell).IsAssignableFrom(myObject.GetType()))

If I am following your question correctly I think what you want to do is make your Card class an interface instead. Then have the Minion inherit from that interface. That will give you better control over what the values are while being able to control them between derived classes.

Sorry if I didn't say things correctly, but none of this answers were helpful. The problem were access modifiers, and all the values those attributes get come from an external program so it wasn't that easy.

Solved my problem using internal keyword, making it only visible from the assembly.

make a interface based design. implement them as per to your need. i know you solved your problem.. but, my suggestion would be rethink the design when you will have some time.

this site might help you to get a good grip on OOP sourcemaking.com

regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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