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. Ubuntu 15.04 安装 Nvidia Quadro系列显卡驱动

    在这之前,我用的Ubuntu都是系统自带的驱动, 由于分辨率没有任何问题, 所以一直没有安装Nvidia官方的驱动; 近期更新到 15.04 之后, 在播放avi 格式的常规视频时却出现闪烁的现象, ...

  2. 十七、Android学习笔记_Android 使用 搜索框

    1.在资源文件夹下创建xml文件夹,并创建一个searchable.xml: android:searchSuggestAuthorityshux属性的值跟实现SearchRecentSuggesti ...

  3. Linux下用freetds连接mssql中文乱码的问题【参考2】

    php5.3的情况下,用pdo的dblib驱动无法连接mssql的,根据官方的描述,5.2已经修改这个bug,5.3没有. 用php自带的mssql函数可以的.编译freetds,php_mssql, ...

  4. 点击文字label同时选中checkbox radio

    在做网页的时候一般会有一个需求:点击一段文字信息的同时选中某个checkbox 旧处理方式是在这段文字上加上点击事件触发checkbox的选中事件 //jq中://选中 $("#ID&quo ...

  5. J2EE5(Servlet2.5)对EL表达式的支持

    JAVA EE5默认 支持EL表达式. 办法一:在每个jsp文件的最上方加入以下代码: <%@ page language="java" import="java. ...

  6. 杭电ACM1170--Balloon Comes!

    地址  http://acm.hdu.edu.cn/showproblem.php?pid=1170 #include<stdio.h> int main() { int t,a,b; ] ...

  7. Android App测试要点

         本文主要内容,转载自 http://www.51testing.com/html/04/344504-849373.html,      在这里,主要是整理一下app测试的总体思路,这里的a ...

  8. 简单版解决IE兼容性问题

    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /> 在访问页面时, 如 ...

  9. 《怎样实现通过shell脚本将用户踢出系统》

    下面是一个将用户踢出系统的脚本: #!/bin/bashread -p "input your username " userps aux | grep "^$user& ...

  10. Hibernate中的对象状态,及自动更新原因

    Hibernate的对象有三种状态,分别为:瞬时状态 (Transient). 持久化状态(Persistent).游离状态(Detached).对它的深入理解,才能更好的理解hibernate的运行 ...