Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Interface 抽象类中的F#部分实现接口_Interface_F#_Abstract Class - Fatal编程技术网

Interface 抽象类中的F#部分实现接口

Interface 抽象类中的F#部分实现接口,interface,f#,abstract-class,Interface,F#,Abstract Class,我试图编写一个部分实现接口的抽象类。我还想为未实现的值设置默认值。似乎F#可能不支持这一点,但同样可能的是,我就是无法正确理解语法。任何帮助都将不胜感激。这就是我目前的处境: type IDrawable = abstract member Position : Vector2 abstract member ShortSymbol : int abstract member ForeColor : string abstract member BackColor

我试图编写一个部分实现接口的抽象类。我还想为未实现的值设置默认值。似乎F#可能不支持这一点,但同样可能的是,我就是无法正确理解语法。任何帮助都将不胜感激。这就是我目前的处境:

type IDrawable =
    abstract member Position : Vector2
    abstract member ShortSymbol : int
    abstract member ForeColor : string
    abstract member BackColor : string
    abstract member Symbol : char
    abstract member Description : string

[<AbstractClass>]
type DrawableBase(position, shortSymbol, foreColor, backColor) =

    let position : Vector2 = position
    let shortSymbol : int = shortSymbol
    let foreColor : string = foreColor
    let backColor : string = backColor

    interface IDrawable with
        member this.Position = position
        member this.ShortSymbol = shortSymbol
        member this.ForeColor = foreColor
        member this.BackColor = backColor

        default this.Symbol = char this.ShortSymbol
        default this.Description = ""

    new(oldBase: DrawableBase, newPosition) = DrawableBase(newPosition, oldBase.ShortSymbol, oldBase.ForeColor, oldBase.BackColor)  
IDrawable类型=
抽象成员位置:Vector2
抽象成员缩写符号:int
抽象成员前景色:字符串
抽象成员背景色:字符串
抽象成员符号:char
抽象成员描述:字符串
[]
类型DrawableBase(位置、短符号、前景色、背景色)=
让位置:矢量2=位置
let shortSymbol:int=shortSymbol
让前景色:字符串=前景色
让backColor:string=backColor
接口IDrawable与
这个成员。位置=位置
成员this.ShortSymbol=ShortSymbol
成员this.ForeColor=ForeColor
成员this.BackColor=BackColor
默认this.Symbol=char this.ShortSymbol
默认值为。Description=“”
新建(oldBase:DrawableBase,newPosition)=DrawableBase(newPosition,oldBase.ShortSymbol,oldBase.ForeColor,oldBase.BackColor)
这是一个学习项目,所以大部分都不重要-我可能在这里也做了一些其他错误-请随意评论。

查看MSDN上的F#接口页面

default
对于类中的接口实现永远无效

然而,再次从文档中

但是,也可以通过包含 将成员作为方法与 默认关键字


我没有听说过部分接口实现,尽管我在F#中使用类型继承和接口做过一些面向对象的工作。在基类中使用虚拟成员可以获得相同的结果:

type IDrawable =
    abstract member Position : Vector2
    abstract member ShortSymbol : int
    abstract member ForeColor : string
    abstract member BackColor : string
    abstract member Symbol : char
    abstract member Description : string

[<AbstractClass>]
type DrawableBase(position, shortSymbol, foreColor, backColor) =

    let position : Vector2 = position
    let shortSymbol : int = shortSymbol
    let foreColor : string = foreColor
    let backColor : string = backColor

    abstract member Symbol : char
    default this.Symbol = char shortSymbol
    abstract member Description : string
    default this.Description = ""

    interface IDrawable with
        member this.Position = position
        member this.ShortSymbol = shortSymbol
        member this.ForeColor = foreColor
        member this.BackColor = backColor

        member this.Symbol = this.Symbol
        member this.Description = this.Description
一种解决方案是将
oldBase
的类型更改为
IDrawable

new(oldBase: IDrawable, newPosition) = DrawableBase(newPosition, oldBase.ShortSymbol, oldBase.ForeColor, oldBase.BackColor) 
另一种解决方案是将所有方法添加到类型本身。这更方便,因为它允许在许多地方避免显式强制转换,并以C#方式(隐式实现接口)处理类,而不会在每个需要调用接口成员的地方造成麻烦
:>

[<AbstractClass>]
type DrawableBase(position, shortSymbol, foreColor, backColor) =

    let position : Vector2 = position
    let shortSymbol : int = shortSymbol
    let foreColor : string = foreColor
    let backColor : string = backColor

    member this.Position = position
    member this.ShortSymbol = shortSymbol
    member this.ForeColor = foreColor
    member this.BackColor = backColor

    abstract member Symbol : char
    default this.Symbol = char shortSymbol
    abstract member Description : string
    default this.Description = ""

    interface IDrawable with
        member this.Position = this.Position
        member this.ShortSymbol = this.ShortSymbol
        member this.ForeColor = this.ForeColor
        member this.BackColor = this.BackColor

        member this.Symbol = this.Symbol
        member this.Description = this.Description

    new(oldBase: DrawableBase, newPosition) = DrawableBase(newPosition, oldBase.ShortSymbol, oldBase.ForeColor, oldBase.BackColor) 
[]
类型DrawableBase(位置、短符号、前景色、背景色)=
让位置:矢量2=位置
let shortSymbol:int=shortSymbol
让前景色:字符串=前景色
让backColor:string=backColor
这个成员。位置=位置
成员this.ShortSymbol=ShortSymbol
成员this.ForeColor=ForeColor
成员this.BackColor=BackColor
抽象成员符号:char
默认值为。Symbol=char shortSymbol
抽象成员描述:字符串
默认值为。Description=“”
接口IDrawable与
成员this.Position=this.Position
成员this.ShortSymbol=this.ShortSymbol
成员this.ForeColor=this.ForeColor
成员this.BackColor=this.BackColor
成员this.Symbol=this.Symbol
成员this.Description=this.Description
新建(oldBase:DrawableBase,newPosition)=DrawableBase(newPosition,oldBase.ShortSymbol,oldBase.ForeColor,oldBase.BackColor)

请注意,在一些罕见的情况下,您可能希望实现接口,而不是作为对类型成员的调用,而是直接作为对字段或私有方法的调用(即复制一些代码,并将其作为
成员this.Position=Position
在接口内部作为类型成员)。这是因为对于类型层次结构,JIT级别的虚拟方法和接口内联并不总是与简单类一样工作,因此调用
member This.Position=This.Position
会产生额外的虚拟调用开销。但这只在您需要进行纳米优化时才起作用,例如,将简单方法的性能从100 Mops提高到150 Mops。

那么这有什么实际不起作用呢?它不喜欢界面下的
default
关键字。
[<AbstractClass>]
type DrawableBase(position, shortSymbol, foreColor, backColor) =

    let position : Vector2 = position
    let shortSymbol : int = shortSymbol
    let foreColor : string = foreColor
    let backColor : string = backColor

    member this.Position = position
    member this.ShortSymbol = shortSymbol
    member this.ForeColor = foreColor
    member this.BackColor = backColor

    abstract member Symbol : char
    default this.Symbol = char shortSymbol
    abstract member Description : string
    default this.Description = ""

    interface IDrawable with
        member this.Position = this.Position
        member this.ShortSymbol = this.ShortSymbol
        member this.ForeColor = this.ForeColor
        member this.BackColor = this.BackColor

        member this.Symbol = this.Symbol
        member this.Description = this.Description

    new(oldBase: DrawableBase, newPosition) = DrawableBase(newPosition, oldBase.ShortSymbol, oldBase.ForeColor, oldBase.BackColor)