(以下基于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不能用于修饰一个类
时间: 2024-10-25 22:41:02