为什么基地只能在私人会员? [英] why is base only possible in private members?

查看:127
本文介绍了为什么基地只能在私人会员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对私人会员之间的区别和让绑定
它可以帮助我澄清我的疑问,为什么这样的东西是不可能的

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 ()   

推荐答案

base 关键字只是真正需要调用一个虚拟方法的基类实现。这是您需要 base 的唯一情况,因为您无法使用 this 实例(因为这将引用当前类中的覆盖)。

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 ()   

标识符 this 只是一个名称 - 类似于成员声明 - 所以你可以使用其他名称。

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

这篇关于为什么基地只能在私人会员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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