遗产多态性.抽象化

本文关键字:抽象化 多态性 遗产 | 更新日期: 2023-09-27 18:16:25

我正在为学生开发一个测试平台应用程序。由于问题可以有一个或多个正确答案,我需要单选按钮/复选框来选择正确的答案。我想用Add方法实现一个抽象类。从中派生出两个类,每个类都包含一个RaddioButtons或Checkboxes数组。有比下面列出的方法更好的方法吗?我的意思是,add方法可以放在抽象类中吗?

public class AnswerForm
    {
        public static int no;
        public AnswerForm()
        {
            no=0;
        }
    }
    public class RadioButtonClass:AnswerForm
    {
        RadioButton[] vector;
        public void Add(RadioButton rbutton)
        {
            vector[no++] = rbutton;
        }
    }
    public class CheckBoxClass : AnswerForm
    {
        CheckBox[] vector;
        public void Add(CheckBox cbox)
        {
            vector[no++] = cbox;
        }
    }

我还有两个向量,我在其中放置了固定数量的元素,单选按钮和复选框。这些元素存在于Windows窗体Form1.cs[design]中。我想做的是将AnswerForm类型的元素传递给函数,然后在函数中,根据我的问题类型,为其中一个派生类的AnswerForm对象分配内存。此外,如果Add方法将s字符串和vector[no++].Text=s作为参数,则可能会更容易;

该功能的原型:

public void readQuestions(RichTextBox richTextBox, AnswerForm answerForm)

在这里,我解析一个XML文件,并将对象放在列表中。XML包含Questions,每个都有一个类型(多个或单个答案(、转到richTextBox的文本以及答案。接下来,我循环浏览问题列表并检查问题的类型。如果有多个答案,则将每个答案放入CheckBox.Text中。否则,将其放入RadioButton.Text中。在将文本分配给每个WinForm元素之前,我想分配相应的对象类型(RadioButtonClass或CheckBoxClass(,然后对当前问题的每个答案使用add方法。这就是为什么我想到了继承,抽象化和多态性。

这就是现在的样子:

public void readQuestions(RichTextBox richTextBox, AnswerForm answerForm)
        {
            var file = XDocument.Load("QuestionsTest.xml");
            var subject = new Subject();
            subject.Name = (string)file.Root.Attribute("Subject");
            var questions = from question in file.Root.Elements("Question")
                            select new Question
                            {
                                NumberOfCorrectAnswers=(int)question.Attribute("NumberOfAnswers"),
                                Text = (string)question.Element("Text"),
                                Answers = new List<Answer>(
                                    from answers in question.Element("Answers").Elements("Answer")
                                    select new Answer
                                    {
                                        Text = (string)answers
                                    })
                            };
            using (var db = new TestingPlatformContext())
            {
                db.Subjects.Add(subject);
                foreach (var question in questions)
                {
                    //Console.WriteLine("Subject: {0}'n Text: {1}", question.Subject, question.Text);
                    richTextBox.Text = question.Text;
                    //db.Questions.Add(question);
                    foreach (var answer in question.Answers)
                        //Console.WriteLine("Answer: {0}", answer.Text);
                        if (question.NumberOfCorrectAnswers != 1)
                        {
                            answerForm = new CheckBoxClass();
                            answerForm.Add(answer.Text);
                            //db.Answers.Add(answer);
                        }
                        else
                        {
                            answerForm = new RadioButtonClass();
                            answerForm.Add(answer.Text);
                        }
                }
            }
        }

遗产多态性.抽象化

是的,您可以使用泛型:将Add((方法移动到父类

public class AnswerForm<T>
{
    private readonly IList<T> _list;
    public AnswerForm()
    {
        _list = new List<T>();
    }
    public void Add(T button)
    {
        _list.Add(button);
    }
}
public class RadioButtonClass:AnswerForm<RadioButton>
{
}
public class CheckBoxClass : AnswerForm<CheckBox>
{
}

我做了一些更改:-使用列表而不是数组,在这种情况下更灵活-在父类AnswerForm 中使用泛型

一个非常简单的解决方案如下:

public class AnswerForm
        {
            public static int no;
            private RadioButton[] rbuttons;
            private Checkbox[] checkboxes;
            public AnswerForm()
            {
                no=0;
                rbuttons = new RadioButton[] 
                {
                    radioButton1,radioButton2,radioButton3,radioButton4,radioButton5
                };
                checkboxes = new CheckBox[]
                {
                    checkBox1,checkBox2,checkBox3,checkBox4,checkBox5,checkBox6
                };
            }
            public void AddRadio(string s)
            {
                rbuttons[no++].Text=s;
            }
            public void AddBox(string s)
            {
                checkboxes[no++].Text=s;
            }
        }

但这远非优雅。