具有以Child类型作为参数的方法的继承的类:错误的方法被调用 [英] Inherited class with methods taking Child type as a parameter: wrong method being called
问题描述
我正在制作纸牌游戏,并且有几个脚本可以控制纸牌行为.
I am making a card game, and I have several scripts that control card behavior.
public class BasicCardModel : Draggable {
public int hitPoints;
public GameObject cardObject;
public static string cardName = "Basic Card";
public CardStatus cardStatus = CardStatus.None;
public void copyAttributes(BasicCardModel bcm) {
Debug.Log("Calling basic copy attributes");
hitPoints = bcm.hitPoints;
}
...
}
我有几张类似于以下内容的专用卡:
I have several specialized cards similar to below:
public class AttackCardModel : BasicCardModel {
public int attackStrength;
public AttackType attackType;
public void copyAttributes(AttackCardModel acm) {
base.copyAttributes(acm);
attackType = acm.attackType;
Debug.Log("Attack strength = " + acm.attackStrength);
attackStrength = acm.attackStrength;
}
}
我有一个生成这些卡的控制器对象:
I have a controller object that generates these cards:
public class GameController : MonoBehaviour {
public GameObject basicCard, attackCard, defenseCard, eventCard, masterCard;
public GameObject topPlayerDeck, topPlayerHand, topPlayerField;
public GameObject bottomPlayerDeck, bottomPlayerHand, bottomPlayerField;
public GameObject eventDeck;
// Use this for initialization
void Start () {
// Link controller to game objects
topPlayerDeck = GameObject.Find("TopPlayerDeck");
topPlayerHand = GameObject.Find("TopPlayerHand");
topPlayerField = GameObject.Find("TopPlayerField");
bottomPlayerDeck = GameObject.Find("BottomPlayerDeck");
bottomPlayerHand = GameObject.Find("BottomPlayerHand");
bottomPlayerField = GameObject.Find("BottomPlayerField");
eventDeck = GameObject.Find("EventDeck");
CardCollection cards = generateCards();
foreach (BasicCardModel card in cards.cards) {
if(card is AttackCardModel) {
createCard<AttackCardModel>(topPlayerHand, card as AttackCardModel, Player.Top, CardStatus.Hand);
createCard<AttackCardModel>(bottomPlayerHand, card as AttackCardModel, Player.Bottom, CardStatus.Hand);
}
else if(card is DefenseCardModel) {
createCard<DefenseCardModel>(topPlayerHand, card as DefenseCardModel, Player.Top, CardStatus.Hand);
createCard<DefenseCardModel>(bottomPlayerHand, card as DefenseCardModel, Player.Bottom, CardStatus.Hand);
}
else {
createCard<BasicCardModel>(topPlayerHand, card as BasicCardModel, Player.Top, CardStatus.Hand);
createCard<BasicCardModel>(bottomPlayerHand, card as BasicCardModel, Player.Bottom, CardStatus.Hand);
}
}
/*
for(int i = 0; i < 2; i++) {
createCard<AttackCardModel>(topPlayerHand, Player.Top, CardStatus.Hand);
createCard<AttackCardModel>(bottomPlayerHand, Player.Bottom, CardStatus.Hand);
}
for (int i = 0; i < 2; i++) {
createCard<DefenseCardModel>(topPlayerHand, Player.Top, CardStatus.Hand);
createCard<DefenseCardModel>(bottomPlayerHand, Player.Bottom, CardStatus.Hand);
}
*/
}
// Update is called once per frame
void Update () {
}
// For testing, have a CardCollection passed in later
public CardCollection generateCards() {
CardCollection cards = new CardCollection();
//AttackCardModel testcard = new AttackCardModel(4, AttackType.SQLInjection, 3);
cards.cards.Add(new AttackCardModel(4, AttackType.SQLInjection, 3));
cards.cards.Add(new DefenseCardModel(5, AttackType.SQLInjection, 1));
//Debug.Log(testcard.attackStrength + "is attack strength");
return cards;
}
public void createCard<T>(GameObject whereToPut, T objectToCopy, Player player, CardStatus cardStatus) where T : BasicCardModel {
GameObject new_card = Instantiate(basicCard);
new_card.transform.SetParent(whereToPut.transform, false);
Destroy(new_card.GetComponent<BasicCardModel>());
new_card.AddComponent<T>();
--->new_card.GetComponent<T>().copyAttributes(objectToCopy); <---
new_card.GetComponent<T>().linkModelToCardObject(new_card);
new_card.GetComponent<T>().setUpCard<T>(player, cardStatus);
}
}
我在此行上遇到麻烦(在控制器对象的结尾附近):
new_card.GetComponent<T>().copyAttributes(objectToCopy);
I am having trouble with this line (near the end of the controller object):
new_card.GetComponent<T>().copyAttributes(objectToCopy);
它不会调用copyAttributes
的子方法,而是调用父方法,因此不会复制我想要的属性.
Instead of calling the child method for copyAttributes
, it calls the parent method, and thus does not copy the attributes that I want.
在我之前关于此的问题中,一个人建议使用动态类型作为解决方案.但是,即使GetComponent<ParentClass>
应该获得子类型,但由于某些原因它也不起作用,因此我需要使用泛型.
In my previous question about this, one person suggested using dynamic typing as a solution. However, even though GetComponent<ParentClass>
should get the child types, it doesn't work for some reason, so I need to use generics.
如何强制它调用子方法而不是父方法?
How can I force it to call the child method instead of the parent method?
推荐答案
将您的BasicCardModel
的copyAttributes
方法设为虚拟.
Make your BasicCardModel
's copyAttributes
method virtual.
public class BasicCardModel : Draggable {
// ...
public virtual void copyAttributes(BasicCardModel bcm) {
// ...
}
// ...
}
然后覆盖它AttackCardModel
,并在复制其他属性之前将卡模型转换为派生类型:
Then override it AttackCardModel
, and cast the card model to the derived type before copying additional attributes:
public override void copyAttributes(BasicCardModel bcm) {
base.copyAttributes(acm);
var acm = bcm as AttackCardModel;
if (acm != null) {
attackType = acm.attackType;
Debug.Log("Attack strength = " + acm.attackStrength);
attackStrength = acm.attackStrength;
}
}
这篇关于具有以Child类型作为参数的方法的继承的类:错误的方法被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!