F#中的类型继承 [英] Type inheritance in F#
本文介绍了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#中的类型继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文