abstract :表示方法是抽象方法,在子类中必须重写。抽象方法所在的类必须是抽象类,即用abstract modifiers;
virtual:表示此方法是virtual方法,除了在子类中可以重写外(在子类中也可直接使用),和普通方法完全一样;
override:表示重写父类的virtual方法;
new: 显式隐藏从基类继承的成员;

区别:

virtual:标记方法为虚方法
1.可在派生类中以override覆盖此方法
2.不覆盖也可由对象调用
3.无此标记的方法(也无其他标记),重写时需用new隐藏原方法
abstract 与virtual : 方法重写时都使用 override 关键字

Eg1:

   public abstract class Book
{
public Book()
{
}
public abstract void getPrice(); //抽象方法,不含主体
public virtual void getName() //虚方法,可覆盖
{
Console.WriteLine("this is a test:virtual getName()");
}
public virtual void getContent() //虚方法,可覆盖
{
Console.WriteLine("this is a test:virtual getContent()");
}
public void getDate() //一般方法,若在派生类中重写,须使用new关键字
{
Console.WriteLine("this is a test: void getDate()");
}
} public class ChineseBook : Book
{
public override void getPrice() //实现抽象方法,必须实现
{
Console.WriteLine("this is a test:ChineseBook override abstract getPrice()");
}
public override void getName() //覆盖原方法,不是必须的
{
Console.WriteLine("this is a test:ChineseBook override virtual getName()");
}
public new void getDate() {
Console.WriteLine("this is a test:ChineseBook new getDate()");
} public void Run() {
getPrice();
getName();
getContent();
getDate();
} }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Output:

this is a test:ChineseBook override abstract getPrice()
this is a test:ChineseBook override virtual getName()
this is a test:virtual getContent()
this is a test:ChineseBook new getDate()

Eg2:

 public abstract class FlowModel
{
public abstract void A(); public virtual void B(){
Console.WriteLine("Orginal B()");
} public virtual void C(){
Console.WriteLine("Orginal C()");
} public void D(){
Console.WriteLine("Orginal D()");
} } public class Flow:FlowModel
{
public override void A()
{
//执行步骤A
Console.WriteLine("Execute Step A ");
} public virtual void B()
{
//执行步骤B
Console.WriteLine("Execute Step B");
} public void C()
{
//执行步骤C
Console.WriteLine("Execute Step C");
} public new void D() {
Console.WriteLine("Execute Step D");
} public void Run()
{
A();
B(); //步骤B是扩展点 ,可以由子类决定具体执行什么
C();
D();
}
}
Output:

Execute Step A
Execute Step B
Execute Step C
Execute Step D

 

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

C# [method Modifiers] abstract virtual override new的更多相关文章

  1. knowing abstract,virtual,override,new

    If a class has at least one member which modified by "abstract",this class is an abstract ...

  2. C# abstract virtual override new finally java final finalize

    virtual:声明虚方法.可以被其派生类所重写的.重写方法需要使用override或者new关键字. override:覆盖原方法.可对重写virtual.override.abstract进行重写 ...

  3. abstract,virtual,override个人

    1.abstract 可以修饰类和方法,修饰方法时只声明不实现: 2.继承实现abstract类必须通过override实现abstract声明的方法,而virtual方法可选择override(重写 ...

  4. abstract,virtual,override

    1.abstract 可以修饰类和方法,修饰方法时只声明不实现: 2.继承实现abstract类必须通过override实现abstract声明的方法,而virtual方法可选择override(重写 ...

  5. Modifiers: virtual, override, new, abstract, sealed, internal

    internal 声明类.类成员.接口或接口成员具有内部可见性. internal 修饰符使类.接口或成员仅在当前包中可见. 当前包之外的代码不能访问 internal 成员.只有在同一程序集的文件中 ...

  6. c#中virtual, abstract和override的区别和用法

    virtual是把一个方法声明为虚方法,使派生类可重写此方法,一般建立的方法是不能够重写的,譬如类A中有个方法protected void method(){ 原代码....;}类B继承自类A,类B能 ...

  7. sealed、new、virtual、abstract与override 趣解

    1. sealed——“断子绝孙” 密封类不能被继承.密封方法可以重写基类中的方法,但其本身不能在任何派生类中进一步重写.当应用于 方法或属性时,sealed修饰符必须始终与override一起使用. ...

  8. sealed、new、virtual、abstract与override 总结

    1. sealed——“断子绝孙” 密封类不能被继承.密封方法可以重写基类中的方法,但其本身不能在任何派生类中进一步重写.当应用于方法或属性时,sealed修饰符必须始终与override一起使用. ...

  9. abstract、override、new、virtual、sealed使用和示例

    abstract修饰类名为抽象类,修饰方法为抽象方法.如果一个类为抽象类,则这个类智能是其他某个类的基类.抽象方法在抽象类中没有函数体.抽象类中的抽象方法是没有方法体的,继承其的子类必须实现抽象类的抽 ...

随机推荐

  1. 高德地图之c#后台获取一个或多个起点到单个终点的直线距离

    首先我们需要一个控制台添加一个新Key(可使用服务选择Web服务,测试的时候IP白名单先不填); 直线距离是通过后台get方式请求API服务地址http://restapi.amap.com/v3/d ...

  2. what eats up the performance in the interior scene?

    - baseline (7w rps/core) - switch from large accelerator to regular accelerator (9w rps/core) - repl ...

  3. DAY31、socket套接字

    一.复习1.网络编程 软件开发架构 b/s架构 c/s架构 本质都是c/s架构2.互联网协议 OSI七层协议 应用层 表示层 会话层 传输层 网络层 数据链路层 物理连接层3. 物理连接层:建立物理连 ...

  4. Python 的web自动化测试

    安装selenium 上面python已安装完毕,接下来安装selenium. 安装selenium之前需安装些必要工具 1. 安装setuptools 下载地址:https://pypi.pytho ...

  5. [ActionScript 3.0] 简单倒计时

    import flash.utils.Timer; import flash.events.TimerEvent; import flash.text.TextField; var text:Text ...

  6. VIM 文档编辑

    VIM进入时默认是普通模式,普通模式下输入“:”,即可进入命令模式,若想进入插入模式,看1:无论什么模式,按Esc键返回普通模式 1. VIM 工作模式 2. VIM 光标操作 3. VIM编辑文档 ...

  7. Oracle 中wmsys.wm_concat拼接字符串,结果过长报错解决

    备忘:这个函数最大是4000,根据拼接列的长度,通过限制拼接条数来防止拼接字符串过长错误 --这个情况是从子表中读取出具,这里直接把它当做查询字段处理,在子表中有所有数据 select info.id ...

  8. struts中如何查看配置文件中是否存在某个返回值

    ActionConfig config = ActionContext.getContext() .getActionInvocation().getProxy().getConfig(); Resu ...

  9. eclipse项目目录展示结构设置

    我因为前后端都搞过, 解除过很多的开发IDE,说真的,很多的项目目录结构都是一级一级分开,然后我可以通过展开等操作来查看文件等资源信息,结果呢?java的开发IDE eclipse默认的项目目录展示简 ...

  10. Android Fragment实现微信底部导航

    1.XML布局 (1)主界面 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout x ...