1 Star 1 Fork 0

ClearStari99/C Sharp 教程

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
038 C Sharp 多态性 最下列笔记 5.cs 961 Bytes
一键复制 编辑 原始数据 按行查看 历史
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();
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/ClearStari99/c-sharp-tutorial.git
git@gitee.com:ClearStari99/c-sharp-tutorial.git
ClearStari99
c-sharp-tutorial
C Sharp 教程
master

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385