虚方法
来源:互联网 发布:数控车内锥编程实例 编辑:程序博客网 时间:2024/04/25 11:54
using System;
namespace VirtualMethod
{
public class Animal
{
public Animal() { }
public void Sleep() { }
public bool IsHungry = true;
public virtual void Eat()
{
Console.WriteLine("Eat something");
}
}
public class Elephant : Animal
{
public int CarryCappacity;
public override void Eat()
{
Console.WriteLine("Eat grass");
}
}
public class Mouse : Animal
{
public override void Eat()
{
Console.WriteLine("Eat cheese");
}
}
public class Cat : Animal
{
public void StalkPrey() { }
public override void Eat()
{
Console.WriteLine("Eat mouse");
}
}
public class WildLife
{
public WildLife()
{
Elephant myElephant = new Elephant();
Mouse myMouse = new Mouse();
FeedingTime(myElephant);
FeedingTime(myMouse);
}
public void FeedingTime(Animal someCreature)
{
if (someCreature.IsHungry) //这里使用了多态性
{
someCreature.Eat();
}
}
}
class Program
{
static void Main(string[] args)
{
WildLife w = new WildLife();
Cat mycat = new Cat();
w.FeedingTime(mycat);
}
}
}
- c#方法-虚方法
- 虚方法、重写方法和抽象方法
- 静态方法,虚方法和动态方法
- 虚方法
- 虚方法
- 虚方法
- 虚方法
- 虚方法
- 虚方法
- 虚方法
- 虚方法
- 虚方法 纯虚方法
- 虚方法&非虚方法
- 方法重新和虚方法
- 抽象方法与虚方法
- 抽象方法和虚方法
- 虚方法与动态方法
- new方法和虚方法
- java面试题oracle
- iOS使用静态链接库(基础篇)
- 线性插值(双线性)(三线性)
- linux 计划任务cron--怎么定时执行的指令(笔记)
- Android权限列表permission说明
- 虚方法
- 高效整洁CSS代码原则
- USB 开发的一些问题
- 运动目标检测、阴影检测及目标跟踪中用得到的标准测试视频下载(大量IBM提供视频)
- 编码问题造成的aix小机上的ftp乱码问题解决
- 留个备份
- redhat5关闭防火墙的方法-chkconfig临时关闭iptables
- 程序员需要具备的基本技能
- websphere参数配置技巧