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

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

问题描述

就见过一个教程说:

 类犬
{
  私人字符串名称;
}
类SuperDog:狗
{
 私人字符串心情;
}

然后是显示该SuperDog将继承名称以及一个UML。我曾尝试但对我来说似乎只有公共成员继承。至少,除非它被声明为public我无法访问名称。


解决方案

  

一个派生类可以访问
  公共,保护,内部和
  碱的保护的内部部件
  类。即使一个派生类
  继承了基础的私有成员
  类,它不能访问的那些成员。
  然而,所有这些私有成员
  还是present在派生类
  可以做他们会在做同样的工作
  基类本身。例如,
  假设一个受保护的基类
  方法访问私有字段。那
  现场已被present派生
  类为使继承的基
  类方法的正常工作。


从:<一href=\"http://msdn.microsoft.com/en-us/library/ms173149.aspx\">http://msdn.microsoft.com/en-us/library/ms173149.aspx

所以,从技术上说,是的,但实际上,没有。

Just seen one tutorial saying that:

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

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.

From: http://msdn.microsoft.com/en-us/library/ms173149.aspx

So, technically, yes, but practically, no.

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

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