【发布时间】:2016-06-24 14:04:57
【问题描述】:
我有一个问题,是否可以从派生类更改基类的字段值。在我的例子中,我有两个类基类,窗口形式为RichTextBox,我想使用派生类来清除RichTextBox。
初始化RichTextBox:
this.rtfCode.Location = new System.Drawing.Point(45, 26);
this.rtfCode.Name = "rtfCode";
this.rtfCode.ShowSelectionMargin = true;
this.rtfCode.Size = new System.Drawing.Size(100, 96);
this.rtfCode.TabIndex = 1;
this.rtfCode.Text = "some text";
基类:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Console.WriteLine(this.rtfCode.Text);
DerivedClass f = new DerivedClass();
Console.WriteLine(f.rtfCode.Text);
}
}
我的派生类
class DerivedClass:Program
{
public DerivedClass()
{
base.rtfCode.Clear();
}
}
当我执行程序并按RichTextBox 中的button 时,我仍然看到文本。
【问题讨论】:
标签: c# class reflection base-class inherited