编写高质量代码改善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< ...
随机推荐
- GroupVarint
folly/GroupVarint.h folly/GroupVarint.h is an implementation of variable-length encoding for 32- and ...
- PCI、PCI-x,PCI-E兼容以及他们之间的区别详细图解
一.PCI PCI接口分为32bit和64bit两种,32bit就是一般台式机使用的普通的pci接口(图一.图三),64bit接口比32bit接口长一些一般只出现在服务器上(图四.图五).32bit和 ...
- http请求头设置
Curl curl -i -H 'Content-Type: application/json' -i/--include 输出时包括protocol头信息curl -x 10.12.15.1 ...
- 关于ie6中绝对定位或浮动的div中既有向左float也有向右float时候如何让外层div自适应宽度的解决方案--
一个详细的说明请见: http://www.cnblogs.com/yiyang/p/3265006.html 我的问题大约为,如下代码: <!DOCTYPE html PUBLIC " ...
- 获取lable选中时触发事件
通常做网页时不会用radio和checkbox的原有样式, 会进行样式美化. 如何在点击checkbox时触发一个事件呢? <div class="main-checkbox" ...
- python web框架简介Bottle Flask Tornado
Bottle Bottle是一个快速.简洁.轻量级的基于WSIG的微型Web框架,此框架只由一个 .py 文件,除了Python的标准库外,其不依赖任何其他模块. ? 1 2 3 4 pip inst ...
- linux 确定网卡接口
方法: ifconfig -a 执行一遍:ifconfig -a: 插上网线 接口会 running. 方法:ethtool 执行 # ethtool -p eth0 时,eth0对应的网口的灯就 ...
- Py小技巧一:在列表,字典,集合中根据条件筛选数据
1.过滤掉列表中的某些项---列表解析 data=[1,4,2,8,5,-1] res=[] a.依次迭代列表中每一个项 for x in data: if >=0: res.append(x) ...
- re模块之research
2. re.research re.research扫描整个字符串并返回第一个成功的匹配. 2.1函数语法: re.search(pattern, string, flags=0) 参数 描述 pat ...
- 用vim生成一批递增ID
假设说要生成1000个以xxx开头的后面加数字的ID,比如xxx1到xxx1000.一般我们可以通过.csv去递增,然后替换,但是直接用vim也是可以达到这样的目的. 下面通过一个gif图演示这个过程 ...