gpt4 book ai didi

c# - 什么是阴影?

转载 作者:太空宇宙 更新时间:2023-11-03 14:56:18 24 4
gpt4 key购买 nike

在 C# 中,术语阴影 是什么意思?我读过this link但没有完全理解。

最佳答案

阴影隐藏了基类中的方法。使用您链接的问题中的示例:

class A 
{
public int Foo(){ return 5;}
public virtual int Bar(){return 5;}
}
class B : A
{
public new int Foo() { return 1;}
public override int Bar() {return 1;}
}

B 重写 虚方法 Bar。它隐藏(隐藏)非虚拟方法Foo。 Override 使用 override 关键字。阴影是通过 new 关键字完成的。

在上面的代码中,如果在 B 类中定义 Foo 方法时没有使用 new 关键字,您将得到这个编译器警告:

'test.B.Foo()' hides inherited member 'test.A.Foo()'. Use the new keyword if hiding was intended.

关于c# - 什么是阴影?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48864015/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com