C#基礎知識整理:基礎知識(7) 方法的隱藏

黄舟
發布: 2017-02-11 13:15:32
原創
1138 人瀏覽過

繼承和抽象類別中提到過,子類別與父類別的方法間有這些關係:
子類別直接使用父類別方法(但是必須父類別方法是public或protected類型);
子類別的方法覆寫父類別方法(override);
子類別的方法重載父類別方法(overload);
看下面這種情況:

    public class YSchool
    {
        private int id = 0;

        private string name = string.Empty;

        public int ID
        {
            get
            {
                return this.id;
            }
        }

        public string Name
        {
            get
            {
                return name;
            }
        }

        public YSchool()
        {
            this.id = 0;

            this.name = @"清华大学附中";
        }

        public  YSchool(int id, string name)
        {
            this.id = id;

            this.name = name;
        }

        /// <summary>
        /// 构造器
        /// </summary>
        public  YSchool(int id)
        {
            this.id = id;

            this.name = @"陕师大附中";
        }
    }

    public class YTeacher
    {
        private int id = 0;

        private string name = string.Empty;

        private YSchool school = null;

        private string introDuction = string.Empty;

        private string imagePath = string.Empty;

        public int ID
        {
            get
            {
                return id;
            }
        }

        public string Name
        {
            get
            {
                return name;
            }
        }

        public YSchool School
        {
            get
            {
                if (school == null)
                {
                    school = new YSchool();
                }
                return school;
            }

            set
            {
                school = value;
            }
        }

        public string IntroDuction
        {
            get
            {
                return introDuction;
            }

            set
            {
                introDuction = value;
            }
        }

        public string ImagePath
        {
            get
            {
                return imagePath;
            }

            set
            {
                imagePath = value;
            }
        }
        /// <summary>
        /// 构造器
        /// </summary>
        public YTeacher(int id, string name)
        {
            this.id = id;

            this.name = name;
        }

        /// <summary>
        /// 构造器
        /// </summary>
        public YTeacher(int id, string name, YSchool school)
        {
            this.id = id;

            this.name = name;

            this.school = school;
        }

        /// <summary>
        /// 给学生讲课的方法
        /// </summary>
        public void ToTeachStudents()
        {
            Console.WriteLine(string.Format(@"{0} 老师教育同学们: Good Good Study,Day Day Up!", this.Name));
        }
        /// <summary>
        /// 惩罚犯错误学生的方法
        /// 加virtual关键字,表示该方法可以被覆盖重写
        /// </summary>
        /// <param name="punishmentContent"></param>
        public virtual void PunishmentStudents(string punishmentContent)
        {
            Console.WriteLine(string.Format(@"{0} 的{1} 老师让犯错误的学生 {2}。", this.School.Name, this.name, punishmentContent));
        }
    }

    public class UniversityTeacher : YTeacher
    {
        public UniversityTeacher(int id, string name,YSchool school)

            : base(id, name, school)
        {

        }

        /// <summary>
        /// 隐藏父类的同名方法,隐藏后该类只能访问隐藏后的方法,不能访问到父类的该方法了。
        /// </summary>
        public new void ToTeachStudents()
        {
            Console.WriteLine(string.Format(@"{0} 老师教育同学们:认真学习.net!", this.Name));
        }
        
        /// <summary>
        /// 覆盖
        /// </summary>
        public override void PunishmentStudents(string punishmentContent)
        {
            base.PunishmentStudents(punishmentContent);//也可以不执行父类方法。

            //自己的代码
        }
    }
登入後複製
using System;

namespace YYS.CSharpStudy.MainConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            UniversityTeacher uTeacher = new UniversityTeacher(3, @"董边成", new YSchool(3, @"清华大学"));

            //访问的是子类方法
            uTeacher.ToTeachStudents();
            //可以访问覆盖后的方法
            uTeacher.PunishmentStudents(@"让你挂科。");

            YTeacher teacher = new UniversityTeacher(3, @"董边成", new YSchool(3, @"清华大学"));
            //访问不到隐藏的那个方法了
            teacher.ToTeachStudents();
            //可以访问覆盖后的方法
            teacher.PunishmentStudents(@"跑10000米。");

            Console.ReadKey();
        }
    }
}
登入後複製

結果:

    子類別繼承父類別的方法,使用new一個相同的方法,使用new父類別方法同名,參數清單相同的新方法的過程就叫做隱藏。也就是子類別隱藏了父類別的這個方法。不過隱藏與覆蓋不同,隱藏的方法只能透過該方法所在的類別訪問,如果使用父類別的變量,依然訪問的是被隱藏的方法。
    從上面的程式碼可以看到,覆蓋和隱藏的差異。父類別變數引用子類別實例後,只能存取被隱藏的方法,而無法存取隱藏後的方法。但是都可以存取到覆蓋後的方法。
還有一點就是如果想讓這個方法被子類別覆蓋,那麼父類該方法必須加上virtual。隱藏父類別的方法new關鍵字也可以不加。隱藏一般使用的比較少,在一些特殊的情況下解決一些問題。

以上就是C#基礎知識整理:基礎知識(7) 方法的隱藏的內容,更多相關內容請關注PHP中文網(www.php.cn)!


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!