C# 中是否继承了私有成员? [英] Are private members inherited in C#?

查看:34
本文介绍了C# 中是否继承了私有成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚刚看到一个教程说:

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

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

Then there was an UML displaying that SuperDog will inherit Name as well. I have tried but to me it seems that only public members are inherited. At least I could not access Name unless it was declared as 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/图书馆/ms173149.aspx

所以,从技术上讲,是的,但实际上,不是.

So, technically, yes, but practically, no.

这篇关于C# 中是否继承了私有成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆