是私人静态和实例变量在C#中,为什么遗传吗? [英] Are private static and instance variables inherited in C# and why?

查看:140
本文介绍了是私人静态和实例变量在C#中,为什么遗传吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读,在基类的私有变量在技术上由子类继承,但无法访问。

I have read that private variables in a base class are technically inherited by child classes, but are not accessible.

如果这是正确的,为什么我们说,他们?当想必他们只能通过反射访问继承

If this is correct, why do we say they are inherited when presumably they can only be accessed by reflection?

推荐答案

子类是关于继承的实施的;和领域是一个实现细节。

Subclassing is about inheriting implementation; and fields are an implementation detail.

字段确实存在,并且是通过反射使用。但最终,它是基类的工作通过任何公共/保护/等成员管理这些领域的状态。

The fields are indeed present, and are available via reflection. But ultimately, it is the base-classes job to manage the state of those fields via any public/protected/etc members.

但最终 - 如果一个基类声明了两个属性(和现场),那么当你设置该属性数据必须去的地方。子类必须包括从基类中的所有字段为它才有意义。这也是至关重要的基于现场的序列化框架(如的BinaryFormatter )。

But ultimately - if a base-class declares a property (and field) for property Foo, then when you set that property the data has to go somewhere. The sub-class has to include all the fields from the base-class for it to make sense. This is also critical for field-based serialization frameworks (such as BinaryFormatter).

这篇关于是私人静态和实例变量在C#中,为什么遗传吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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