C# 与成员隐藏
在下面的例子中,会发生什么?
class Base {
public int abc = 3;
}
Class Derived : Base {
public int abc = 2;
}
static void Main() {
Derived blah = new Derived();
Console.WriteLine(blah.abc);
}
我确信您会在控制台上看到“2”,但我正在阅读(和看到)的内容与此相反......
为什么您会看到“3”而不是“2”?我认为派生类的成员“隐藏”了基类的相同成员......
in the below example, what would happen?
class Base {
public int abc = 3;
}
Class Derived : Base {
public int abc = 2;
}
static void Main() {
Derived blah = new Derived();
Console.WriteLine(blah.abc);
}
I'm sure you would see '2' on your console, but what I'm reading (and seeing) opposes that...
Why would you see '3' instead of '2'? I thought members of derived classes 'hid' the same members of base classes...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,您有一些编译器错误,但这里是基于您的示例的更正版本。
第一行将输出
2
。第二行将输出3
。原因是,除非您显式重写基类的成员,否则它将仅适用于具体类的实例。在派生类中,您实质上使用的是
new
修饰符关键字而不明确使用它。new
关键字隐藏一个基成员,但是,如果将具体类强制转换为其基类型,则新属性不会被使用,也无法访问,直到它被使用为止。再次被“贬低”到具体阶级。在第二个示例中,
Derived
类被强制转换为Base
,因此它将使用Base
abc
属性。如果您要使用覆盖
关键字 ,那么第二行也会输出2
。编辑:请记住,为了允许在
Derived
类上使用override
,您需要标记Base.abc
与virtual
关键字 。另外,您不能将字段虚拟
。您需要使用属性来使用虚拟
关键字。不过,您不应该一开始就公开字段,因此这通常不是问题。Well, you have a few compiler errors, but here is a corrected version based on your example.
The first line will output a
2
. The second line will output a3
. The reason why is because, unless you explicitly override a member of a base class, it will only apply to instances of the concrete class.In your derived class, you're essentially using the
new
modifier keyword without explicitly using it. Thenew
keyword hides a base member, however if the concrete class is cast as its base type, the new property doesn't get used and can't be accessed until it is "cast-down" to the concrete class again.In the second example, the
Derived
class is cast as aBase
, so it will use theBase
abc
property. If you were to use theoverride
keyword, then the second line would also output a2
.Edit: Keep in mind that in order to be allowed to use
override
on theDerived
class, you need to markBase.abc
with thevirtual
keyword. Also, you can't make fieldsvirtual
. You need to use Properties to use thevirtual
keyword. You shouldn't be exposing fields to begin with though, so this is generally never a problem.忽略代码中明显且大量的错误:O,您的断言“我确信您会在控制台上看到'2'”是正确的,您会的,我也会的!
您应该真正使用
如果隐藏继承值是您的意图,
Ignoring the obvious and numerous errors in your code :O, your assertion that "I'm sure you would see '2' on your console" is correct, you will, and I do!
You should really user
if hiding the inherited value is your intention
当然您会看到2。您为什么认为您会看到 3 ?
但你会收到警告:
所以你可以解决它:
Of course you will see 2. Why did you think that you will see 3 ?
But you will get a warning:
So you can solve it doing: