new 与override 区别
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Newoverride
{
class Dad
{
public Dad()
{
Console.WriteLine("Dad construtor");
}
public virtual void method()
{
Console.WriteLine("Dad method");
}
}
class SmallSon : Dad
{
public SmallSon()
{
Console.WriteLine("Smallson construtor");
}
public override void method()
{
Console.WriteLine("override Smallson method");
}
}
class BigSon : Dad
{
public BigSon()
{
Console.WriteLine("BigSon construtor");
}
public new void method()
{
Console.WriteLine("new BigSon method ");
}
}
class Program
{
static void Main(string[] args)
{
Dad f = new Dad();
f.method();
Dad f1 = (Dad)new SmallSon();// 先初始化Dad(),然后再初始化SmallSon()
f1.method();//override smallson method
Dad f2 = (Dad)new BigSon();// 先初始化Dad(),然后再初始化Bigson()
f2.method();// Dad method
BigSon s = new BigSon();
s.method();
}
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步