代码拉取完成,页面将自动刷新
using System;
class Animal
{
public virtual void Eat()
{
Console.WriteLine("这是Animal的Eat方法");
}
}
class Dog:Animal
{
public override void Eat()
{
Console.WriteLine("这是Dog的Eat方法");
}
}
class Cat:Animal
{
public override void Eat()
{
Console.WriteLine("这是Cat的Eat方法");
}
}
class Program
{
static void Main(string[] args)
{
// 多态的实现前提是继承和方法重写,基类可以new所有的派生类,new哪个类,它就执行那个类的方法
Animal animal1 = new Animal(); // 实例Animal类
Animal animal2 = new Dog(); // 实例Dog类
Animal animal3 = new Cat(); // 实例Cat类
animal1.Eat(); // 调用Animal类的Eat方法
animal2.Eat(); // 调用Dog类的Eat方法
animal3.Eat(); // 调用Cat类的Eat方法
Console.ReadKey();
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。