惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News | PayPal Newsroom
云风的 BLOG
云风的 BLOG
GbyAI
GbyAI
Engineering at Meta
Engineering at Meta
B
Blog RSS Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Register - Security
The Register - Security
L
LangChain Blog
A
About on SuperTechFans
S
Schneier on Security
博客园 - 三生石上(FineUI控件)
Stack Overflow Blog
Stack Overflow Blog
The Hacker News
The Hacker News
AWS News Blog
AWS News Blog
博客园 - 司徒正美
Scott Helme
Scott Helme
K
Kaspersky official blog
Cyberwarzone
Cyberwarzone
T
Tenable Blog
腾讯CDC
Recorded Future
Recorded Future
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
G
GRAHAM CLULEY
Security Latest
Security Latest
S
Securelist
D
Darknet – Hacking Tools, Hacker News & Cyber Security
aimingoo的专栏
aimingoo的专栏
Google DeepMind News
Google DeepMind News
V
Vulnerabilities – Threatpost
雷峰网
雷峰网
T
The Exploit Database - CXSecurity.com
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
V2EX
T
The Blog of Author Tim Ferriss
D
Docker
S
Security Affairs
F
Full Disclosure
Know Your Adversary
Know Your Adversary
N
News and Events Feed by Topic
N
News and Events Feed by Topic
T
Tor Project blog
Hugging Face - Blog
Hugging Face - Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Microsoft Security Blog
Microsoft Security Blog
Simon Willison's Weblog
Simon Willison's Weblog
Recent Announcements
Recent Announcements
博客园_首页
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Security @ Cisco Blogs

博客园 - KevinWang

Server.HtmlEncode vs HttpUtility.HtmlEncode 在FireFox IE 下Response 中文文件名乱码问题 序列化,反序列化时低序位非打印 ASCII 字符的问题 - KevinWang 月薪上万的人都懂得这些道理 c# .ToString()格式化 利用ListView和DataPager控件来对数据分页 PageMethods Not Found (or Defined)? (转) asp.net中使用ajax中的三种方式 Asp.Net2.0技巧 - KevinWang - 博客园 数据库设计经验之谈(转载) 网站令浏览器崩溃的原因 各种流行的编程风格 2009年海外Web设计风潮 常用js框架,js库 硬盘RAID是什么意思?有什么用? T-SQL和PL/SQL 区别 What is PL/SQL?(PL/SQL是什么,与T-SQL对比) 数据库和数据仓库的区别 ASP.NET MVC 资料
虚方法(virtual)和抽象方法(abstract)的区别
KevinWang · 2010-02-02 · via 博客园 - KevinWang

虚方法和抽象方法都可以供派生类重写,它们之间有什么区别呢?

1. 虚方法必须有实现部分,抽象方法没有提供实现部分,抽象方法是一种强制派生类覆盖的方法,否则派生类将不能被实例化。如:

//抽象方法
public abstract class Animal
{
    
public abstract void Sleep();
    
public abstract void Eat();
}
//虚方法
public class Animal
{
    
public virtual void Sleep(){}
    
public virtual void Eat(){}
}

2. 抽象方法只能在抽象类中声明,虚方法不是。其实如果类包含抽象方法,那么该类也是抽象的,也必须声明为抽象的。如:

public class Animal
{
    
public abstract void Sleep();
    
public abstract void Eat();
}

编译器会报错:
Main.cs(10): 'VSTest.Animal.Sleep()' is abstract but it is contained in nonabstract class 'VSTest.Animal'
Main.cs(11): 'VSTest.Animal.Eat()' is abstract but it is contained in nonabstract class 'VSTest.Animal'

3. 抽象方法必须在派生类中重写,这一点跟接口类似,虚方法不必。如:

public abstract class Animal
{
    
public abstract void Sleep();
    
public abstract void Eat();
}
public class Cat : Animal
{
    
public override void Sleep()
    {
        Console.WriteLine( 
"Cat is sleeping" );
    }
    
// we need implement Animal.Eat() here

}


编译器会报错:Main.cs(14): 'VSTest.Cat' does not implement inherited abstract member 'VSTest.Animal.Eat()',因为我们没有实现抽象类中所有抽象方法。