关于继承类是否需要强制转换的为题

lightheartedfrog lightheartedfrog 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
dog dog = new dog();
animal animal = (animal)dog;// 问题在此处:为何去掉(animal)后结果一样。是不是意味着基类引用指向继承实例时不需要强制转换?有隐式转换?
animal.eatfood();
}
}

class animal
{
virtual public void eatfood()
{
Console.WriteLine("this is animal eat food");
Console.ReadKey();

}
}

class dog : animal
{
override public void eatfood()
{
Console.WriteLine("this is dog eat food");
Console.ReadKey();
}
}
}
...全文

相关推荐

评论 11
收藏 已收藏
分享
返回顶部
lightheartedfrog
C# 111,128 成员 642,537 内容