why is base only possible in private members?

匆匆过客 提交于 2019-12-11 04:08:44

问题


I have some understanding of the difference between private members and let bindings. It may help me clarify my doubts understanding why something like this is not possible

type B () =
    inherit A ()

    let doSomething () =
        base.CallToA ()   

Is it to prevent partially constructed objects or some leaks during construction?


回答1:


The base keyword is only really needed to call a base-class implementation of a virtual method. That is the only case where you need base because you cannot invoke the method using the this instance (as that would refer to the override in the current class).

You are partially correct that the compiler wants to prevent you from accessing partially constructed objects though. However, this is done by requiring you to explicitly say that you want to be able to refer to the current instance inside the constructor using as this:

type B () as this =
  inherit A ()

  let doSomething () =
    this.CallToA ()   

The identifier this is just a name - similarly to member declarations - so you could use other name there.



来源:https://stackoverflow.com/questions/22121335/why-is-base-only-possible-in-private-members

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!