小编Mik*_*Liu的帖子

继承和覆盖时,对象声明中的c#new

例如,

public class Foo
{
    public virtual bool DoSomething() { return false; }
}

public class Bar : Foo
{
    public override bool DoSomething() { return true; }
}

public class Test
{
    public static void Main()
    {
        Foo test = new Bar();
        Console.WriteLine(test.DoSomething());
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么答案是真的?"新酒吧()"是什么意思?"new Bar()"是不是意味着分配内存?

c# polymorphism inheritance overriding new-operator

0
推荐指数
1
解决办法
277
查看次数

标签 统计

c# ×1

inheritance ×1

new-operator ×1

overriding ×1

polymorphism ×1