如何在包含子类的超类列表上调用子类方法

本文关键字:列表 调用 类方法 超类 包含 子类 | 更新日期: 2023-09-27 18:16:53

我在写标题时遇到了困难,所以现在让我试着详细说明一下。首先是我的相关代码:

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 == "answerswer")
                    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);
        }
    }

所以我创建了多种类型的问题都是"问题"的子类。Question包含适用于每种类型问题的所有数据,这些子类包含它们独有的方法,主要方法是"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的定义,尽管它的每个子类都包含了。请评论,如果我应该添加更多的澄清,我的代码已经得到了一点混乱,因为我已经反复修改它。我对这样的课程很陌生,所以我可能会错过一些关键的概念,请指出你看到的任何东西,谢谢。

如何在包含子类的超类列表上调用子类方法

创建基类abstract,在基类中添加一个抽象的WriteQuestion成员,然后在每个具体实现中override

我认为我会把你的代码分成更多的类,以获得一个很好的关注点分离。

你的Question类和专门化(即派生类)不应该知道它们是如何存储的,也不应该知道它们是如何转换成某种格式的,如HTML表示。

我将定义一个名为XmlQuestionConverter的类:
public class XmlQuestionConverter
{   
    public XmlQuestionConverter() 
    {
        TypeToConvertMap = new Dictionary<Type, Action<Question, XElement>>
        {
            { typeof(TrueFalseQuestion), new Action<Question, XElement>(ConvertTrueFalseFromXml) }
            // other mappings...
        };
    }
    private Dictionary<Type, Action<Question, HtmlTextWriter>> TypeToConvertMap
    {
        get;
    }
     // This dictionary maps element names to their type
     private Dictionary<string, Type> QuestionTypeMap { get; } = new Dictionary<string, Type>()
     {
          { "truefalse", typeof(TrueFalseQuestion) },
          { "multichoice", typeof(MultiChoiceQuestion) }
          // And so on
     };
     public IList<Question> ConvertFromXml(XDocument questionsDocument)
     {
        // This will get all question elements and it'll project them
        // into concrete Question instances upcasted to Question base
        // class
        List<Question> questions = questionsDocument
                     .Descendants("question")
                     .Select
                     (
                        element =>
                        {
                           Type questionType = QuestionTypeMap[q.Attribute("type").Value];
                           Question question = (Question)Activator.CreateInstance(questionType);
                           // Calls the appropiate delegate to perform specific
                           // actions against the instantiated question
                           TypeToConvertMap[questionType](question, element);
                           return question;
                        }
                     ).ToList();
        return questions;
     }
     private void ConvertTrueFalseFromXml(TrueFalseQuestion question, XElement element)
     {
          // Here you can populate specific attributes from the XElement
          // to the whole typed question instance!
     }
}

现在您可以将XDocument转换为问题列表,我们准备用HtmlTextWriter:

将它们转换为HTML。
public class HtmlQuestionConverter
{
    public HtmlQuestionConverter() 
    {
        TypeToConvertMap = new Dictionary<Type, Action<Question, HtmlTextWriter>>
        {
            { typeof(TrueFalseQuestion), new Action<Question, HtmlTextWriter>(ConvertTrueFalseToHtml) }
            // other mappings...
        };
    }
    private Dictionary<Type, Action<Question, HtmlTextWriter>> TypeToConvertMap
    {
        get;
    }
    public void ConvertToHtml(IEnumerable<Question> questions, HtmlTextWriter htmlWriter)
    {
        foreach (Question question in questions)
        {
            // Calls the appropiate method to turn the question
            // into HTML using found delegate!
            TypeToConvertMap[question.GetType()](question, htmlWriter);
        }
    }
    private void ConvertTrueFalseToHtml(Question question, HtmlTextWriter htmlWriter)
    {
        // Code to write the question to the HtmlTextWriter...
    }
}

这样的话,我不认为你需要多态性:

XmlQuestionConverter xmlQuestionConverter = new XmlQuestionConverter();
IList<Question> questions = xmlQuestionConverter.ConvertFromXml(xdoc);
HtmlQuestionConverter htmlQuestionConverter = new HtmlQuestionConverter();
htmlQuestionConverter.ConvertToHtml(questions, htmlWriter);

注意:我不能尝试执行这段代码,我不能100%确定它是否会工作,但这是一个很好的开始,了解如何实现你的代码与一个清晰的关注点分离!您可能需要做一些调整,使我的代码适应您的实际用例