[英]Inheritance. Polymorphism. Abstractization
我正在为学生开发一个测试平台应用程序。 由于问题可以有一个或多个正确答案,因此我需要单选按钮/复选框来选择正确的答案。 我想用Add方法实现一个抽象类。 从中派生两个类,每个类包含一个RaddioButton
或Checkbox
数组。 是否有比下面列出的方法更好的方法? 我的意思是,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;
}
}
我还有两个向量,其中放置了固定数量的元素,即RadioButtons和Checkboxes。 这些元素存在于Windows Form 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;
}
}
但这远非优雅。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.