我很難說出我的標題,所以現在讓我試着闡述一下。首先這裏是我的相關代碼:如何在包含子類的超類列表上調用子類方法
class Question
{
static bool checkFile(XElement q)
{
foreach (XElement a in q.Descendants())
{
if (a.Name.LocalName == "file")
{
return true;
}
}
return false;
}
protected string questionText;
protected List<File> files;
protected Question question;
public Question(XElement q)
{
questionText = q.Element("questiontext").Element("text").Value.ToString();
string name = q.Attribute("type").Value.ToString();
if (checkFile(q))
files.Add(new File(q));
}
}
class multichoice : Question
{
private List<string> answers;
public multichoice(XElement q)
: base(q)
{
foreach (XElement a in q.Elements())
{
if (a.Name.LocalName == "answer")
answers.Add(a.Element("text").Value.ToString());
}
}
public void writeQuestion(HtmlTextWriter writer)
{
writer.RenderBeginTag("p");
writer.Write("<strong>Multiple Choice: </strong>" + this.questionText);
writer.RenderEndTag();
writer.AddAttribute("type", "A");
writer.RenderBeginTag("ol");
foreach (string answer in answers)
{
writer.RenderBeginTag("li");
writer.Write(answer);
writer.RenderEndTag();
}
writer.RenderEndTag();
}
}
class truefalse : Question
{
public truefalse(XElement q)
: base(q)
{
}
public void writeQuestion(HtmlTextWriter writer)
{
writer.RenderBeginTag("strong");
writer.Write("True or False : ");
writer.RenderEndTag();
writer.Write(questionText);
}
}
所以我創建了多種類型的問題,都是「問題」的子類。問題包含適用於每種類型問題的所有數據,這些子類包含它們獨有的方法,主要是「writeQuestion」。 現在我試圖用它做的是這樣的:
static List<Question> collectQuestions(XDocument doc)
{
XDocument xdoc = doc;
string elementName = null;
List<Question> questions = null;
foreach (XElement q in xdoc.Descendants("question"))
{
elementName = q.Attribute("type").Value.ToString();
if (elementName != "category")
continue;
if (elementName == "truefalse")
questions.Add(new truefalse(q)); //writeTrueFalse(writer, q);
else if (elementName == "calculatedmulti")
questions.Add(new calculatedmulti(q)); // writeCalculatedMulti(writer, q);
else if (elementName == "calculatedsimple")
questions.Add(new calculatedsimple(q)); // writeCalculatedSimple(writer, q);
else if (elementName == "ddwtos")
questions.Add(new Draganddrop(q)); //writeDragAndDrop(writer, q);
else if (elementName == "description")
questions.Add(new Description(q)); // writeDescription(writer, q);
else if (elementName == "essay")
questions.Add(new Essay(q)); // writeEssay(writer, q);
else if (elementName == "gapselect")
questions.Add(new Gapselect(q)); // writeGapSelect(writer, q);
else if (elementName == "matching")
questions.Add(new Matching(q)); // writeMatching(writer, q);
else if (elementName == "multichoice")
questions.Add(new multichoice(q)); // writeMultipleChoice(writer, q);
else if (elementName == "multichoiceset")
questions.Add(new Allornothing(q)); // writeAllOrNothing(writer, q);
else if (elementName == "numerical")
questions.Add(new Numerical(q)); // writeNumerical(writer, q);
else if (elementName == "shortanswer")
questions.Add(new shortanswer(q)); // writeShortAnswer(writer, q);
else
continue;
}
return questions;
}
questions = collectQuestions(someDocument);
foreach (Question question in questions)
{
question.writeQuestion(writer);
}
有沒有辦法來調用每個項目的writeQuestion?現在它當然會給出這樣的錯誤:即使它的每個子類都存在,Questions也不包含writeQuestion的定義。請評論,如果我應該補充更多的澄清,我的代碼已經有點糾結,因爲我一再重複它。對於像這樣的課程,我很體面,所以我可能會錯過一些關鍵概念,請指出您看到的任何內容,謝謝。
爲什麼在Question超類中沒有'writeQuestion'方法?添加一個或爲此創建一個接口。 – RBarryYoung
順便說一句,即使Eric Lippert正確地解決了你的問題,我也看到了一個不明確的問題分離:你將類和它們如何表示爲HTML。這是一個非常糟糕的主意。 –
@Matias Fidemraizer,我的完整代碼正在做的是解析一個xml文件並將其重新格式化爲html文件中所需的格式。我在這些問題中存儲我需要的數據以及代表每個問題的代碼塊。然後我輸出這些塊到文件。到目前爲止,它運作良好。這是什麼讓這是一個壞主意,你會建議什麼? – Zannith