博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#基础知识整理:基础知识(7) 方法的隐藏
阅读量:7052 次
发布时间:2019-06-28

本文共 4785 字,大约阅读时间需要 15 分钟。

继承和抽象类中提到过,子类与父类的方法间有这些关系:

子类直接使用父类方法(但是必须父类方法是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;          }            ///           /// 构造器          ///           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;              }          }          ///           /// 构造器          ///           public YTeacher(int id, string name)          {              this.id = id;                this.name = name;          }            ///           /// 构造器          ///           public YTeacher(int id, string name, YSchool school)          {              this.id = id;                this.name = name;                this.school = school;          }            ///           /// 给学生讲课的方法          ///           public void ToTeachStudents()          {              Console.WriteLine(string.Format(@"{0} 老师教育同学们: Good Good Study,Day Day Up!", this.Name));          }          ///           /// 惩罚犯错误学生的方法          /// 加virtual关键字,表示该方法可以被覆盖重写          ///           ///           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)          {            }            ///           /// 隐藏父类的同名方法,隐藏后该类只能访问隐藏后的方法,不能访问到父类的该方法了。          ///           public new void ToTeachStudents()          {              Console.WriteLine(string.Format(@"{0} 老师教育同学们:认真学习.net!", this.Name));          }                    ///           /// 覆盖          ///           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://pasol.baihongyu.com/

你可能感兴趣的文章
Python: The _imagingft C module is not installed错误的解决
查看>>
HTTP请求报文和HTTP响应报文
查看>>
第3课 - 初识程序的灵魂
查看>>
WordPress插件扫描工具plecost
查看>>
【PDF】Java操作PDF之iText超入门
查看>>
PHP:第五章——字符串过滤函数
查看>>
Spring中ApplicationContextAware的用法
查看>>
flask的session解读及flask_login登录过程研究
查看>>
ElasticSearch单机多实例环境部署
查看>>
python 练习
查看>>
Centos 安装 nload
查看>>
python3简单使用requests
查看>>
由一次java作业 引起的思考
查看>>
HDU 3389 Game(博弈)
查看>>
仅IE支持clearAttributes/mergeAttributes方法
查看>>
Linux中U盘和SD卡加载卸载命令
查看>>
github push403错误的处理
查看>>
Hibernate与 MyBatis的比较
查看>>
关于百度地图API的地图坐标转换问题
查看>>
【操作系统】设备管理(五)
查看>>