gpt4 book ai didi

c# - 私有(private)成员是否在 C# 中继承?

转载 作者:IT王子 更新时间:2023-10-29 03:46:19 25 4
gpt4 key购买 nike

刚刚看到一个教程说:

Class Dog
{
private string Name;
}
Class SuperDog:Dog
{
private string Mood;
}

然后有一个 UML 显示 SuperDog 也将继承 Name。我试过了,但对我来说似乎只有公共(public)成员是继承的。至少我无法访问 Name,除非它被声明为公共(public)。

最佳答案

A derived class has access to the public, protected, internal, and protected internal members of a base class. Even though a derived class inherits the private members of a base class, it cannot access those members. However, all those private members are still present in the derived class and can do the same work they would do in the base class itself. For example, suppose that a protected base class method accesses a private field. That field has to be present in the derived class in order for the inherited base class method to work properly.

发件人:http://msdn.microsoft.com/en-us/library/ms173149.aspx

因此,从技术上讲,是的,但实际上不是。

关于c# - 私有(private)成员是否在 C# 中继承?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2950820/

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