Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Inheritance 在自定义文本框上调用基本文本方法_Inheritance - Fatal编程技术网

Inheritance 在自定义文本框上调用基本文本方法

Inheritance 在自定义文本框上调用基本文本方法,inheritance,Inheritance,我正在尝试创建一个从TextBox继承的CurrencyTextBox。我看到了一些非常奇怪的行为,我只是不明白 经过大量测试,我想我可以总结如下: 在类代码中,当我访问base.Text(以获取textbox的文本)时,实际上是获取重写文本属性的返回值 我认为base关键字可以确保调用底层对象的方法 证明: public class cTestTextBox : System.Windows.Forms.TextBox { string strText = "";

我正在尝试创建一个从TextBox继承的CurrencyTextBox。我看到了一些非常奇怪的行为,我只是不明白

经过大量测试,我想我可以总结如下:

在类代码中,当我访问base.Text(以获取textbox的文本)时,实际上是获取重写文本属性的返回值

我认为base关键字可以确保调用底层对象的方法

证明:


public class cTestTextBox : System.Windows.Forms.TextBox
    {
        string strText = "";

        public cTestTextBox()
        {
            SetVal("AAA");
            base.Text = "TEST";
        }
        public override string Text
        {
            get
            {
                string s = strText;
                s = "++" + s + "++";
                return s;
            }
        }
        public void SetVal(string val)
        {
            strText = val;
        }
    }
将此控件放置在窗体上,并在构造函数上设置断点。运行应用程序。将鼠标悬停在base.Text表达式上。请注意,工具提示显示的是替代特性的值,而不是基本特性的值。执行SetVal()语句,然后再次将鼠标悬停在base.Text表达式上。请注意,工具顶部显示的是重写属性的值,而不是基本属性的值


如何可靠地访问从中继承的textbox的Text属性?

很有趣。不确定“被删除”的帖子发生了什么,但他可能已经走上了正确的轨道。若要查看某个基类是否正在调用.Text,请在.Text中放入某些内容,如果发生这种情况,则会导致无限循环:

public override string Text
{
    get
    {
         return base.Text; 
    }
}