如果派生类继承了基类的私有成员,那么为什么不是构造函数呢? [英] If derived class inherits the private members of a base class, then why not constructors?

查看:151
本文介绍了如果派生类继承了基类的私有成员,那么为什么不是构造函数呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在c#中清楚地理解这个基本的OOPS概念。在大多数互联网网站上,我读到派生类继承基类的私有成员,但无法访问这些成员。

I want to clear my understanding of this basic OOPS concept in c#. On most of the internet sites, I read that a derived class inherits the private members of a base class, but it cannot access those members.


派生类可以访问基类的public,protected,internal和
protected internal成员。即使派生的
类继承了基类的私有成员,它也无法访问
这些成员。但是,所有这些私有成员仍然存在于
派生类中,并且可以执行与基本
类本身相同的工作。例如,假设受保护的基类方法
访问私有字段。该字段必须存在于派生的
类中,以使继承的基类方法正常工作。

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

我的问题是,如果我们认为上面的内容是正确的,那么我们可以说基类的构造函数在派生类中继承,但是派生类只能使用base关键字通过自己的构造函数访问/调用它,并且在创建派生类的实例时,外部世界将无法使用此构造函数。

My question is, if we consider above is correct, then can we say "Constructors of base class are inherited in derived class, but derived class can only access/call it through its own constructor using base keyword and this constructor will not be available to outside world while creating instance of derived class".

public class Employee
{
    public int salary;

    public Employee(int annualSalary)
    {
        salary = annualSalary;
    }
}

public class Manager : Employee
{
    public Manager(int annualSalary)
        : base(annualSalary)
    {
        //Add further instructions here.
    }
}

因为要调用基类构造函数,它应该是出现内部该类。也许我的解释是错误的。有人可以解释一下吗?

Because to call a base class constructor, it should be present inside that class. Maybe my interpretation is wrong. Can anyone please explain this?

提前致谢!

推荐答案

这取决于你如何定义现在。如果将其定义为某处可用,则基类中的私有成员以及构造函数都是存在的。如果将present定义为在该特定类中找到,则两者都不是存在。

It depends on how you define "present". If you define it as "somewhere available", private members in base classes are "present" as well as constructors. If you define "present" as "found in that particular class", both are not "present".

尝试使用反射。您将找不到基类的任何私有成员。私有成员是继承的,因此可用,但仍然只在基类中。

Try using reflection. You won't find any private members from base classes. The private members are inherited, thus available, but still only in the base class.

所以是构造函数。

    class A
    {
        private A(int i) { }
        public A() { }
        private void Foo() { }
        public void Bar() { }
    }

    class B : A
    {

    }

    var aProperties = typeof(A).GetMembers(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.FlattenHierarchy);
    // you won't see Foo in this line, nor any constructors of A
    var bProperties = typeof(B).GetMembers(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public  | BindingFlags.FlattenHierarchy);

最后,你可以说:


  • 基类的所有成员都以某种方式可用于执行。

  • 没有语法可以从继承类(也不是从任何地方)调用私有成员(课外)els

  • 只能使用 base 关键字从构造函数中调用基类的构造函数。 (构造函数始终从层次结构中的每个基类调用。如果未指定,则它是默认构造函数。)

  • 仅声明成员(或被一个类覆盖,实际上是在内部那个特定的类中找到的。使用反射,您可以使用 BindingFlags.FlattenHierarchy 来展平基类中的可见成员。私有成员和构造函数只能在声明类中找到。

  • All members of base classes are present in terms of somehow available for execution.
  • There is no syntax to call private members from inheriting classes (nor from anywhere els outside the class)
  • Constructors of the base class can only be called from constructors using the base keyword. (A constructor is always called from each base class in the hierarchy. If not specified, it is the default constructor.)
  • Only members that are declared (or overridden) by a class are actually found "inside" that particular class. Using reflection, you can use BindingFlags.FlattenHierarchy to flatten visible members from base classes. Private members and constructors are only found in the declaring class.

这篇关于如果派生类继承了基类的私有成员,那么为什么不是构造函数呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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