我知道使用的是基本属性,而不是“我的”属性。有办法改变这种情况吗?
嗯,不是真的。问题是,它不是一个虚拟财产,而且你刚刚引入了一个同名的新财产——它没有被称为多态财产。
当然,更简单的解决方案就是
设置
这个
Text
财产你自己。。。可能在构造函数中:
public class QuestionNode : TreeNode
{
private readonly string question;
public QuestionNode(string question)
{
this.question = question;
this.Text = string.Format("Question: {0}", question);
}
}
你
能够
当然,可以在属性setter中执行此操作:
public class QuestionNode : TreeNode
{
private string question;
public string Question
{
get { return question; }
set
{
this.question = value;
this.Text = string.Format("Question: {0}", question);
}
}
}
……但就我个人而言,我会坚持使用构造函数版本,除非你有什么原因需要稍后更改问题。