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

抽象类,抽象函数

时间:2017-08-25 13:48:50      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:void   apple   over   prot   class   ide   rac   使用   水果   

什么时候使用抽象函数?
首先抽象函数必然在抽象类中,以父类的形式出现。
当子类均具有相同方法时,可以由于父类定义。
当不同部分子类有不同方法时,可以override该方法。但是,调用时必须以子类的形态出现,否则依然调用父类方法。
为避免出现子类父类方法调用不清的问题,将父类的方法设置为抽象类。子类单独定义即可避免调用父类方法。
案例:吃水果的流程
abstract class Fruit
{
    protected override void Eat();
}
 
class Apple : Fruit
{
    public override void Eat()
    {
        EatWithTooth();
    }
}
 
class Pinapple : Fruit
{
    public override void Eat()
    {
        Decladding();
        EatWithTooth();
    }
}
 
void Main()
{
    Fruit apple = Pinapple();
  
}

--- Lan (sunlyk@QQ.com)

抽象类,抽象函数

标签:void   apple   over   prot   class   ide   rac   使用   水果   

原文地址:http://www.cnblogs.com/sunlyk/p/7427330.html

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