码迷,mamicode.com
首页 > 其他好文 > 详细

abstract 和 virtual的区别

时间:2014-10-27 12:39:45      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:io   ar   for   sp   strong   on   cti   bs   as   

(以下基于C#)

1.对方法修饰的区别

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.

一个abstract方法没有方法体,子类必须实现被abstract修饰的方法

写法:

public abstract void DoWork();

一个virtual方法可以有一个基本的实现,子类可以选择性的重写方法

写法:即使是没有实现也和上面不同,必须用{}来代替

public virtual void DoWork(){}

2.对类的修饰

abstract可以修饰一个class,这样做,和不加abstract修饰的唯一区别是,添加abstract后,可以定义abstract方法

virtual不能用于修饰一个类

abstract 和 virtual的区别

标签:io   ar   for   sp   strong   on   cti   bs   as   

原文地址:http://www.cnblogs.com/zhangrCsharp/p/4053777.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!