`
zhangziyangup
  • 浏览: 1072204 次
文章分类
社区版块
存档分类
最新评论

C#基础知识整理:基础知识(7) 方法的隐藏

 
阅读更多

继承和抽象类中提到过,子类与父类的方法间有这些关系:
子类直接使用父类方法(但是必须父类方法是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修饰一个同父类方法同名,参数列表相同的新方法的过程就叫做隐藏。也就是子类隐藏了父类的这个方法。不过隐藏与覆盖不同,隐藏的方法只能通过该方法所在的类访问,如果使用父类的变量,依然访问的是被隐藏的方法。
从上面的代码中可以看到,覆盖和隐藏的区别。父类变量引用子类实例后,只能访问被隐藏的方法,而无法访问隐藏后的方法。但是都可以访问到覆盖后的方法。
还有一点就是如果想让这个方法被子类覆盖,那么父类该方法必须加上virtual。隐藏父类的方法new关键字也可以不加。隐藏一般使用的比较少,在一些特殊的情况下解决一些问题。

代码下载:http://download.csdn.net/detail/yysyangyangyangshan/4399465

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics