码迷,mamicode.com
首页 > Windows程序 > 详细

C# this和base的使用

时间:2014-12-05 19:08:20      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   使用   sp   on   

namespace THISORBASE
{
    //参考地址:http://blog.sina.com.cn/s/blog_7300c7d90100rs20.html
    /*这个时候,派生类和基类的_str被区分开了,
     * 派生类定义了一个自己_str(注意定义的时候,
     * 请加上new关键字,虽然不加也可以编译通过,
     * 但是推荐加上),这样this真正的价值得到了体现。
     * this,指向到了派生类对象的自己定义的字段。
     * base才是指向的基类的字段。这就是C#.net 里 this和base的真正意义。
*/
    public class Father
    {
        protected string _str = "父亲对象字符串的初始值";
        public Father()
        {
            this._str = "父亲对象构造函数该变量字符串初始值";
        }
    }
    public class Son : Father
    {
        private new string _str = "子类对象字符串初始值";
        public Son()
        {
            base._str = "子类对象构造函数修改父类初始化";
        }
        public string PrintStr_this()
        {
            return this._str;
        }
        public string PrintStr()
        {
            return _str;
        }
        public string PrintStr_base()
        {
            return base._str;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Son myobj = new Son();
            Console.WriteLine(myobj.PrintStr_this ());
            Console.WriteLine(myobj .PrintStr() );
            Console.WriteLine(myobj .PrintStr_base() );
            Console.Read();
        }
    }
}

 

C# this和base的使用

标签:style   blog   http   io   ar   color   使用   sp   on   

原文地址:http://www.cnblogs.com/e3e4/p/4147117.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!