![](/img/trans.png)
[英]I have class A, which contains class B, I want to use class A in class B without instantiating class A
[英]If i have a Array of parent Class A which contains child classes A and B is there any way i can call properties exclusive to A and B from the list
我現在正在編寫紙牌游戲,我在陣列中有一副類型為Card的紙牌。
所有卡都是怪物卡或咒語卡,但是當我將它們放在列表中時,我不知道如何從列表中激活它們的方法,例如,怪物卡具有方法攻擊,其中父類別和另一個子咒語卡才不是。
我不能做Deck[5].Attack(int Target)
。 我該怎么辦?
如果Deck [5]是一張可能是怪獸的卡,則可以在調用.Attack()之前嘗試將其轉換為怪獸類型。
代替
Deck[5].Attack(int Target)
,
Monster m = (Deck[5] as Monster);
if (m!=null) {
m.Attack(Target)
}else{
// not a monster
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.