简体   繁体   English

访问修饰符C#

[英]Access modifiers C#

I have a class Card and it has some attributes that get their value from an external API: 我有一个Card类,它具有一些可从外部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 ) 然后我有一个从Card继承的Minion类( 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? 我面临的问题是,显然我可以从Minion访问这些成员(因为它们是公共的),甚至没有任何意义( Minion.IsPlayer吗?显然不是),所以我的问题是:我如何保留这些成员仅适用于Card类而不适用于其子级?

The private modifier won't grant me access later in other part of the library so I can't use it. private修饰符稍后不会授予我在该库其他部分的访问权限,因此我无法使用它。

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. 拥有具有IsCard属性的Card对象没有太大意义,这也是为什么Minion对象上的IsMinion属性也没有意义的原因。 Those properties don't really belong to a Card object. 这些属性实际上并不属于Card对象。

I suggest rethinking your design. 我建议重新考虑您的设计。 It looks as if this Card class is an enum containing all those player types: 好像这个Card类是一个包含所有那些玩家类型的enum

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. 在OOP中,大量关于创建适合问题的模型的问题。

Some things to note: 注意事项:

Inheritance is an "is-a" relationship. 继承是“是”关系。 Minion : Card means "a Minion is a Card ". Minion : Card意思是“ MinionCard ”。 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. 您已经可以使用该语言询问对象是Card还是Minion还是Spell There are a few ways to do this: 有几种方法可以做到这一点:

Using the is keyword: ( link ) 使用is关键字:( link

if (myObject is Spell)

Comparing Type s: ( Type typeof ) 比较Type s :( Type typeof

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

Using the IsAssignableFrom method ( link ): 使用IsAssignableFrom方法( 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. 如果我正确地遵循了您的问题,我想您想做的就是让Card类成为接口。 Then have the Minion inherit from that interface. 然后让Minion从该接口继承。 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. 使用internal关键字解决了我的问题,使其仅在程序集中可见。

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 这个网站可以帮助您更好地掌握OOP sourcemaking.com

regards 问候

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

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