C# 在抽象列表中添加项目
C# add item in abstract list
对于一个项目,我正在使用 intherentance 和抽象 class。在表单的帮助下,我想将项目添加到列表中,但在编码过程中出现以下错误:无法创建实例抽象类型或接口 'Article'。有人知道如何修理他的吗?
文章管理员class:
private List<Article> items;
public ArticleManager()
{
items = new List<Article>();
}
public void addArticleEmergency(Article emergencyNews)
{
items.Add(emergencyNews);
}
文章摘要class:
abstract class Article
{
public int id { get; private set; }
public string title { get; private set; }
public string text { get; private set; }
public Article(int id, string title, string text)
{
this.id = id;
this.title = title;
this.text = text;
}
public bool HasID(int id)
{
if (id == this.id)
{
return true;
}
return false;
}
public override string ToString()
{
return id + ": \r\n" + title + " \r\n " + text;
}
}
}
形式:
private ArticleManager articalManagerAdd;
public Form1()
{
InitializeComponent();
this.articalManagerAdd = new ArticleManager();
}
private void btnMakeNewsArticle_Click(object sender, EventArgs e)
{
if(txtNewsNumber.Text == "" || txtNewsTitle.Text == "" || txtNewsText.Text == "" || !rbEmergency.Checked && !rbNormal.Checked )
{
lbSeeNewsItem.Items.Clear();
lbSeeNewsItem.Items.Add("Please fill in all the required information");
}
else
{
if (articalManagerAdd.GetArticle(Convert.ToInt32(txtNewsNumber.Text)) == null)
{
if (rbNormal.Checked)
{
articalManagerAdd.addArticleNormal(new Article(Convert.ToInt32(txtNewsNumber.Text), txtNewsTitle.Text, txtNewsText.Text));
MessageBox.Show("Normal news article has been added");
}
else if(rbEmergency.Checked)
{
Article emergencyNews = new NewsArticle(Convert.ToInt32(txtNewsNumber.Text), txtNewsTitle.Text, txtNewsText.Text);
MessageBox.Show("Emergency news article has been added");
}
}
else
{
lbSeeNewsItem.Items.Add("This id has already been used");
}
}
}
如评论所述,您不能创建抽象实例 class。
因此您的代码 new Article(Convert.ToInt32(txtNewsNumber.Text), txtNewsTitle.Text, txtNewsText.Text))
将无法编译。
您可以从非抽象 class 继承,因此如果您刚刚删除 [=] 的 abstract
修饰符,您的 NewsArticle
class 应该可以正常工作13=] class.
或者,您可以添加 Article
的新子 class 说 NormalArticle
并在您的 addArticleNormal
方法中使用它
对于一个项目,我正在使用 intherentance 和抽象 class。在表单的帮助下,我想将项目添加到列表中,但在编码过程中出现以下错误:无法创建实例抽象类型或接口 'Article'。有人知道如何修理他的吗?
文章管理员class:
private List<Article> items;
public ArticleManager()
{
items = new List<Article>();
}
public void addArticleEmergency(Article emergencyNews)
{
items.Add(emergencyNews);
}
文章摘要class:
abstract class Article
{
public int id { get; private set; }
public string title { get; private set; }
public string text { get; private set; }
public Article(int id, string title, string text)
{
this.id = id;
this.title = title;
this.text = text;
}
public bool HasID(int id)
{
if (id == this.id)
{
return true;
}
return false;
}
public override string ToString()
{
return id + ": \r\n" + title + " \r\n " + text;
}
}
}
形式:
private ArticleManager articalManagerAdd;
public Form1()
{
InitializeComponent();
this.articalManagerAdd = new ArticleManager();
}
private void btnMakeNewsArticle_Click(object sender, EventArgs e)
{
if(txtNewsNumber.Text == "" || txtNewsTitle.Text == "" || txtNewsText.Text == "" || !rbEmergency.Checked && !rbNormal.Checked )
{
lbSeeNewsItem.Items.Clear();
lbSeeNewsItem.Items.Add("Please fill in all the required information");
}
else
{
if (articalManagerAdd.GetArticle(Convert.ToInt32(txtNewsNumber.Text)) == null)
{
if (rbNormal.Checked)
{
articalManagerAdd.addArticleNormal(new Article(Convert.ToInt32(txtNewsNumber.Text), txtNewsTitle.Text, txtNewsText.Text));
MessageBox.Show("Normal news article has been added");
}
else if(rbEmergency.Checked)
{
Article emergencyNews = new NewsArticle(Convert.ToInt32(txtNewsNumber.Text), txtNewsTitle.Text, txtNewsText.Text);
MessageBox.Show("Emergency news article has been added");
}
}
else
{
lbSeeNewsItem.Items.Add("This id has already been used");
}
}
}
如评论所述,您不能创建抽象实例 class。
因此您的代码 new Article(Convert.ToInt32(txtNewsNumber.Text), txtNewsTitle.Text, txtNewsText.Text))
将无法编译。
您可以从非抽象 class 继承,因此如果您刚刚删除 [=] 的 abstract
修饰符,您的 NewsArticle
class 应该可以正常工作13=] class.
或者,您可以添加 Article
的新子 class 说 NormalArticle
并在您的 addArticleNormal
方法中使用它