F#中的类型继承

编程入门 行业动态 更新时间:2024-10-22 20:43:08
本文介绍了F#中的类型继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我找不到合适的语法来编码类型D,该类型D继承了基类B(用C#编写)及其除基类隐式构造函数之外的构造函数:

I can't find the proper syntax for coding a type D that inherits a base class B (written in C#) and his constructors other than the base class implicit constructor:

C#代码:

public class B { private int _i; private float _f; public B() { _i = 0; _f = 0.0f; } public B(int i) { _i = 0; _f = 0.0f; } public B(int i, float f) { _i = i; _f = f; } }

F#代码:

type D() = inherit B() //how to inherit from other constructors ?

谢谢

推荐答案

由于这个博客!

type D = class inherit B new () = { inherit B() } new (i : int) = { inherit B(i) } new ((i,f) : int*single) = { inherit B(i, f) } end

是的,这有点麻烦,但就像Brian所说的那样,并不是大多数情况。

Yes, it's a bit cumbersome, but like Brian said it's not the majority of the cases.

编辑:实际上,class / end关键字不是强制性的(因此,我收回了我所说的麻烦)。 正如Brian在其博客此处所述, F#通常会推断所定义的类型,使这些令牌不必要/多余。

Actually, class/end keywords are not mandatory for that (so I take back what i said about cumbersomeness). As Brian said on his blog here, F# normally infers the kind of type being defined, making these tokens unnecessary/redundant.

type D = inherit B new () = { inherit B() } new (i : int) = { inherit B(i) } new ((i,f) : int*single) = { inherit B(i, f) }

更多推荐

F#中的类型继承

本文发布于:2023-07-30 15:16:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1250557.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!