编写高质量代码改善C#程序的157个建议——建议94:区别对待override和new
建议94:区别对待override和new
override和new使类型体系应为继承而呈现出多态性。多态要求子类具有与基类同名的方法,override和new的作用就是:
- 如果子类中的方法前面带有new关键字,则该法被定义为独立于基类的方法。
- 如果子类中的方法前面带有override关键字,则子类的对象将调用该方法。而不调用基类的方法。
我们来看一个继承体系:
public class Shape
{
public virtual void MethodVirtual()
{
Console.WriteLine("base MethodVirtual call");
} public void Method()
{
Console.WriteLine("base Method call");
}
} class Circle : Shape
{
public override void MethodVirtual()
{
Console.WriteLine("circle override MethodVirtual");
}
} class Rectangle : Shape
{ } class Triangle : Shape
{
public new void MethodVirtual()
{
Console.WriteLine("triangle new MethodVirtual");
} public new void Method()
{
Console.WriteLine("triangle new Method");
}
} class Diamond : Shape
{
public void MethodVirtual()
{
Console.WriteLine("Diamond default MethodVirtual");
} public void Method()
{
Console.WriteLine("Diamond default Method");
}
}
Shape是所有子类的基类。
Circle类override父类的MethodVirtual,所以即使子类转型为Shape,调用的还是子类方法:
Shape s = new Circle();
s.MethodVirtual();
s.Method();
输出为:
circle override MethodVirtual
base Method call
Circle s = new Circle();
s.MethodVirtual();
s.Method();
输出也为:
circle override MethodVirtual
base Method call
类型Rectangle没有对基类做任何处理,所以无论是否转型为Shape,调用的都是基类Shape的方法。
类型Triangle将基类Shape的virtual方法和非virtual方法都new了一般,所以第一种方法为:
Shape s = new Triangle();
s.MethodVirtual();
s.Method();
因为子类应经new了父类的方法,故子类方法和基类方法完全没有关系了,只要s被转型为Shape,针对s调用搞得都是父类方法。
Triangle triangle = new Triangle();
triangle.MethodVirtual();
triangle.Method();
调用的都是子类方法,输出为:
triangle new MethodVirtual
triangle new Method
类型Diamond包含了两个和基类一模一样的方法,并且没有额外的修饰符。这在编译器中会提出警示。但是如果选择忽略这些警示,程序还是一样可以运行。
Shape s=new Diamond();
s.MethodVirtual();
s.Method();
编译器会默认new的效果,所以输出和显示设置为new时一样。
输出为:
base MethodVirtual call
base Method call
Diamond s = new Diamond();
s.MethodVirtual();
s.Method();
输出为:
Diamond default MethodVirtual
Diamond default Method
最后给一个综合示例:
static void Main(string[] args)
{
TestShape();
TestDerive();
TestDerive2();
} private static void TestShape()
{
Console.WriteLine("TestShape\tStart");
List<Shape> shapes = new List<Shape>();
shapes.Add(new Circle());
shapes.Add(new Rectangle());
shapes.Add(new Triangle());
shapes.Add(new Diamond());
foreach (Shape s in shapes)
{
s.MethodVirtual();
s.Method();
}
Console.WriteLine("TestShape\tEnd\n");
} private static void TestDerive()
{
Console.WriteLine("TestDerive\tStart");
Circle circle = new Circle();
Rectangle rectangle = new Rectangle();
Triangle triangel = new Triangle();
Diamond diamond = new Diamond();
circle.MethodVirtual();
circle.Method();
rectangle.MethodVirtual();
rectangle.Method();
triangel.MethodVirtual();
triangel.Method();
diamond.MethodVirtual();
diamond.Method();
Console.WriteLine("TestShape\tEnd\n");
} private static void TestDerive2()
{
Console.WriteLine("TestDerive2\tStart");
Circle circle = new Circle();
PrintShape(circle);
Rectangle rectangle = new Rectangle();
PrintShape(rectangle);
Triangle triangel = new Triangle();
PrintShape(triangel);
Diamond diamond = new Diamond();
PrintShape(diamond);
Console.WriteLine("TestDerive2\tEnd\n");
} static void PrintShape(Shape sharpe)
{
sharpe.MethodVirtual();
sharpe.Method();
}
输出为:
TestShape Start
circle override MethodVirtual
base Method call
base MethodVirtual call
base Method call
base MethodVirtual call
base Method call
base MethodVirtual call
base Method call
TestShape End
TestDerive Start
circle override MethodVirtual
base Method call
base MethodVirtual call
base Method call
triangle new MethodVirtual
triangle new Method
Diamond default MethodVirtual
Diamond default Method
TestShape End
TestDerive2 Start
circle override MethodVirtual
base Method call
base MethodVirtual call
base Method call
base MethodVirtual call
base Method call
base MethodVirtual call
base Method call
TestDerive2 End
转自:《编写高质量代码改善C#程序的157个建议》陆敏技
编写高质量代码改善C#程序的157个建议——建议94:区别对待override和new的更多相关文章
- 编写高质量代码改善C#程序的157个建议[1-3]
原文:编写高质量代码改善C#程序的157个建议[1-3] 前言 本文主要来学习记录前三个建议. 建议1.正确操作字符串 建议2.使用默认转型方法 建议3.区别对待强制转换与as和is 其中有很多需要理 ...
- 读书--编写高质量代码 改善C#程序的157个建议
最近读了陆敏技写的一本书<<编写高质量代码 改善C#程序的157个建议>>书写的很好.我还看了他的博客http://www.cnblogs.com/luminji . 前面部 ...
- 编写高质量代码改善C#程序的157个建议——建议157:从写第一个界面开始,就进行自动化测试
建议157:从写第一个界面开始,就进行自动化测试 如果说单元测试是白盒测试,那么自动化测试就是黑盒测试.黑盒测试要求捕捉界面上的控件句柄,并对其进行编码,以达到模拟人工操作的目的.具体的自动化测试请学 ...
- 编写高质量代码改善C#程序的157个建议——建议156:利用特性为应用程序提供多个版本
建议156:利用特性为应用程序提供多个版本 基于如下理由,需要为应用程序提供多个版本: 应用程序有体验版和完整功能版. 应用程序在迭代过程中需要屏蔽一些不成熟的功能. 假设我们的应用程序共有两类功能: ...
- 编写高质量代码改善C#程序的157个建议——建议155:随生产代码一起提交单元测试代码
建议155:随生产代码一起提交单元测试代码 首先提出一个问题:我们害怕修改代码吗?是否曾经无数次面对乱糟糟的代码,下决心进行重构,然后在一个月后的某个周一,却收到来自测试版的报告:新的版本,没有之前的 ...
- 编写高质量代码改善C#程序的157个建议——建议154:不要过度设计,在敏捷中体会重构的乐趣
建议154:不要过度设计,在敏捷中体会重构的乐趣 有时候,我们不得不随时更改软件的设计: 如果项目是针对某个大型机构的,不同级别的软件使用者,会提出不同的需求,或者随着关键岗位人员的更替,需求也会随个 ...
- 编写高质量代码改善C#程序的157个建议——建议153:若抛出异常,则必须要注释
建议153:若抛出异常,则必须要注释 有一种必须加注释的场景,即使异常.如果API抛出异常,则必须给出注释.调用者必须通过注释才能知道如何处理那些专有的异常.通常,即便良好的命名也不可能告诉我们方法会 ...
- 编写高质量代码改善C#程序的157个建议——建议152:最少,甚至是不要注释
建议152:最少,甚至是不要注释 以往,我们在代码中不写上几行注释,就会被认为是钟不负责任的态度.现在,这种观点正在改变.试想,如果我们所有的命名全部采用有意义的单词或词组,注释还有多少存在的价值. ...
- 编写高质量代码改善C#程序的157个建议——建议151:使用事件访问器替换公开的事件成员变量
建议151:使用事件访问器替换公开的事件成员变量 事件访问器包含两部分内容:添加访问器和删除访问器.如果涉及公开的事件字段,应该始终使用事件访问器.代码如下所示: class SampleClass ...
- 编写高质量代码改善C#程序的157个建议——建议150:使用匿名方法、Lambda表达式代替方法
建议150:使用匿名方法.Lambda表达式代替方法 方法体如果过小(如小于3行),专门为此定义一个方法就会显得过于繁琐.比如: static void SampeMethod() { List< ...
随机推荐
- 在CentOS 7中使用VS Code编译调试C++项目
1. 安装VSCODE 见VSCode官方链接 https://code.visualstudio.com/docs/setup/linux#_rhel-fedora-and-centos-based ...
- Git学习之常用的命令
配置git git config --global user.name "你的github用户名" git config --global user.email "你的G ...
- js控制手机震动
js控制手机震动 <button onclick="vibrate()">震动</button> <script> function vibra ...
- django -- url (模版语言 {% url 'test1' param1=5 param2=6 %})
如果想让form表单提交的url是类似 action="/index-5-6.html" 这样的,可以在html模版语言中使用{% url 'test1' param1=5 par ...
- windows下Mysql5.7.10免安装版配置
免安装配置: 在环境变量 Path 中追加 %mysql_home%\bin; 配置mysql目录下的 my-default.ini 文件,在mysql 根目录下新建 data 文件夹 使用管理员权限 ...
- 用Pylint规范化Python代码,附PyCharm配置
Pylint一个可以检查Python代码错误,执行代码规范的工具.它还可以对代码风格提出建议. 官网:https://pylint.readthedocs.io pip install pylint ...
- C# ValueTypes
[C# ValueTypes] 1.哪些类型是ValueType? The value types consist of two main categories: Structs Enumeratio ...
- Ros学习——movebase源码解读之amcl
1.amcl的cmakelists.txt文件 add_executable(amcl src/amcl_node.cpp) target_link_libraries(amcl amcl_sens ...
- java命令查询属性信息
System.getProperty("user.home")可以查询JAVA系统的user.home属性的值, 除了user.home,还有user.dir, file.sepa ...
- 346. Moving Average from Data Stream数据窗口流中位数的数据结构设计
[抄题]: Given a stream of integers and a window size, calculate the moving average of all integers in ...