An abstract function has to be overridden while a virtual function may be overridden.

Virtual functions can have a default /generic implementation in the base class.

An abstract function can have no functionality. You're basically saying, any child class MUST give their own version of this method, however it's too general to even try to implement in the parent class. A virtual function, is basically saying look, here's the functionality that may or may not be good enough for the child class. So if it is good enough, use this method, if not, then override me, and provide your own functionality.

public class BaseClass
{
public void SayHello()
{
Console.WriteLine("Hello");
}
public virtual void SayGoodbye()
{
Console.WriteLine("Goodbye");
}
public void HelloGoodbye()
{
this.SayHello();
this.SayGoodbye();
}
}
public class DerivedClass : BaseClass
{
public new void SayHello()
{
Console.WriteLine("Hi There");
}
public override void SayGoodbye()
{
Console.WriteLine("See you later");
}
}

When I instantiate DerivedClass and call SayHello, or SayGoodbye, I get "Hi There" and "See you later". If I call HelloGoodbye, I get "Hello" and "See you later". This is because SayGoodbye is virtual, and can be replaced by derived classes. SayHello is only hidden, so when I call that from my base class I get my original method.

Abstract methods are implicitly virtual. They define behavior that must be present, more like an interface does.

这里new 关键字的作用

在用作修饰符时,new 关键字可以显式隐藏从基类继承的成员。隐藏继承的成员意味着该成员的派生版本将替换基类版本。在不使用 new 修饰符的情况下隐藏成员是允许的,但会生成警告。使用 new 显式隐藏成员会取消此警告,并记录代之以派生版本这一事实。若要隐藏继承的成员,请使用相同名称在派生类中声明该成员,并使用 new 修饰符修饰该成员

http://stackoverflow.com/questions/391483/what-is-the-difference-between-an-abstract-function-and-a-virtual-function\

http://msdn.microsoft.com/zh-cn/library/sf985hc5.aspx

http://msdn.microsoft.com/zh-cn/library/9fkccyh4.aspx

http://msdn.microsoft.com/en-us/library/aa645767(v=vs.71).aspx

http://msdn.microsoft.com/en-us/library/aa664435(v=vs.71).aspx

C# abstract function VS virtual function?的更多相关文章

  1. [C++] Pure Virtual Function and Abstract Class

    Pure Virtual Function Abstract Class

  2. 纯虚函数(pure virtual function )和抽象类(abstract base class)

    函数体=0的虚函数称为“纯虚函数”.包含纯虚函数的类称为“抽象类” #include <string> class Animal // This Animal is an abstract ...

  3. (转) Virtual function

    原文地址:http://en.wikipedia.org/wiki/Virtual_function In object-oriented programming, a virtual functio ...

  4. why pure virtual function has definition 为什么可以在基类中实现纯虚函数

    看了会音频,无意搜到一个frameworks/base/include/utils/Flattenable.h : virtual ~Flattenable() = 0; 所以查了下“纯虚函数定义实现 ...

  5. pure virtual function call

    2015-04-08 10:58:19 基类中定义了纯虚函数,派生类中将其实现. 如果在基类的构造函数或者析构函数中调用了改纯虚函数, 则会出现R6205 Error: pure virtual fu ...

  6. Mindjet MindManager 2012 从模板创建出现“Runtime Error pure virtual function call” 解决方法

    我的Mindjet MindManager 2012 Pro也就是MindManager10 在应用模板之后总会显示 Microsoft Visual C++ Runtime Library Runt ...

  7. OD: Windows Security Techniques & GS Bypassing via C++ Virtual Function

    Windows 安全机制 漏洞的万源之本在于冯诺依曼设计的计算机模型没有将代码和数据进行区分——病毒.加壳脱壳.shellcode.跨站脚本攻击.SQL注入等都是因为计算机把数据和代码混淆这一天然缺陷 ...

  8. OD: Memory Attach Technology - Off by One, Virtual Function in C++ & Heap Spray

    Off by One 根据 Halvar Flake 在“Third Generation Exploitation”中的描述,漏洞利用技术依攻击难度从小到大分为三类: . 基础的栈溢出利用,可以利用 ...

  9. [c++] polymorphism without virtual function

    polymorphism without virtual function

随机推荐

  1. activity传递数据

    这些都是老生常谈了,到处都搜的到,但是因为经常忘记,放着好调用: 传递数据: Intent intent = new Intent(); Bundle bundle = new Bundle(); b ...

  2. MapView

    有两种方式可以将 MapView 添加到应用当中:一个是 XML 方式,另一个是硬编码方式:一般多采用 XML方式,方便调整布局及其属性相关设置. 1.1XML方式 <com.esri.andr ...

  3. 关于SVN下载代码和Android Studio里面导入别人代码build tools不一致问题总结

    项目build tools的sdk我没有 网络问题没法下载. 解决问题 方法 1.下载对应的SDK.需要VPN,或者FQ 2.将bulid.gradle的内容用下面的内容替换 // Top-level ...

  4. php中sprintf与printf函数用法区别

    下面是一个示例:四舍五入保留小数点后两位  代码如下 复制代码 <?php$num1 = 21;echo sprintf("%0.2f",$num1)."<b ...

  5. 个人收集的iOS开源动画-----长期跟新

    1. KYAnimatedPageControl 链接:KYAnimatedPageControl KYAnimatedPageControl 不仅可以在你滑动UIScrollView的时候自动以动画 ...

  6. Mongo客户端MongoVUE的基本使用

    这里没有涉及到服务器以及客户端的安装,文章主要介绍mongo客户端mongoVUE的使用 一.数据库连接 点击绿色加号添加一个连接,输入name.server.port,点击save,点击connec ...

  7. HDU1070 Milk 细节决定成败

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1070 注意:1.喝到第五天,第六天就不喝了  2.相同花费的,优先考虑容量大的  3.注意强制类型转换 ...

  8. hibernate 一对多映射关系

    1.   单项多对一映射       custom(顾客)与order(订单) :一个顾客可以有多个订单,一个订单只能有一个顾客       配置方法:在多的一端配置<many -to one& ...

  9. WindowsPhone8 数据库增删改查

    今天第一次在博客园发表文章,如果有的地方写的不对,还请大家指出! 1.这就是一个简单wp8数据库增删改查 1.创建数据表Person [Table] public class Person : INo ...

  10. SQL联合查询中的关键语法(转)

    联合查询效率较高.以下例子来说明联合查询的好处 t1表结构(用户名,密码)    userid int         username   varchar(20)     password      ...