我正在嘗試編寫一個程序來模擬紙牌遊戲。我有一個名爲MagicCard的類,以及擴展卡的各種類,比如card1,card2 ...我想找到一種方法來存儲不同卡片(卡片)的列表/數組。每個card1,card2 ...都有一個「特殊」效果,當它們播放時它們會觸發,所以需要一個獨特的功能,這就是爲什麼我需要所有不同的子類。許多牌具有相似的屬性。我怎樣才能以合理和方便的方式儲存甲板? (我想讓甲板很容易被操控,這樣我就可以添加一張卡片,或者當我想要時從中移除一張卡片)。存儲不同子類的列表/數組的最佳方式
目前,這是我所:
//My Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace mtgDeckSimulator
{
//General properties of a magic card, extended by individual classes
class cardProperties
{
private String cardName;
/*Mana Array Aranged in the following Order:
* [Colorless,White,Blue,Black,Red,Green] */
private int[] manaCost;
/* Card Type int definitions:
* 0-Artifact
* 1-Creature
* 2-Enchantment
* 3-Instant
* 4-Land
* 5-Planeswalker
* 6-Sorcery
* 7-Tribal */
private int cardType;
public int attack;
public int defense;
private bool permBool;
private bool isLegen; //Or is restricted to one copy
public cardProperties(String cardName,int[] manaCostIn,int cardTypeIn,int attack,int defense,bool permBoolIn,bool isLegenIn)
{
this.cardName = cardName;
manaCost = manaCostIn;
cardType = cardTypeIn;
permBool = permBoolIn;
isLegen = isLegenIn;
}
//Get Variables
public String getCardName()
{
return cardName;
}
public int[] getManaCost()
{
return manaCost;
}
public int getCardType()
{
return cardType;
}
public bool getPermBool()
{
return permBool;
}
public bool getIsLegen()
{
return isLegen;
}
//Other Methods
}
//Interface for magic cards
interface MagicCard
{
int SpecialEffect();
}
#region Magic Card List
class WallOfOmens : cardProperties, MagicCard
{
public WallOfOmens()
: base("Wall of Omens", new int[6] { 1, 1, 0, 0, 0, 0 }, 1, 0, 4, true, false)
{
;
}
public int SpecialEfect()
{
return 1;
//throw new NotImplementedException();
}
}
class Card2 : cardProperties, MagicCard
{
public int SpecialEfect()
{
return 2;
//throw new NotImplementedException();
}
}
#endregion
}
有沒有實現這更好的辦法?代碼也不起作用,因爲它告訴我WallOfOmens和Card2不實現SpecialEffect(),爲什麼會發生這種情況?
如果您在界面中將'SpecialEfect'拼寫爲'SpecialEffect',它可能會更好。 – 2010-11-19 19:27:58
因爲你並不是說你的函數實現了這個接口。他們應該被稱爲MagicCard.SpecialEffect() – Stu 2010-11-19 19:28:29
@Stu:在C#中,除非它不明確(即,具有相同成員的多個接口),否則不需要限定該接口 – 2010-11-19 19:29:34