当前位置:  开发笔记 > 编程语言 > 正文

如何在F#中实现抽象类?

如何解决《如何在F#中实现抽象类?》经验,为你挑选了1个好方法。



1> Brian..:

这是一些示例代码

type IBaz =
    abstract member Baz : int -> int

[]
type MyAbsClass() =
    // abstract
    abstract member Foo : int -> int
    // virtual (abstract with default value)
    abstract member Bar : string -> int
    default this.Bar s = s.Length 
    // concrete
    member this.Qux x = x + 1

    // a way to implement an interface
    abstract member Baz: int -> int
    interface IBaz with
        member this.Baz x = this.Baz x

type MySubClass(z : int) =
    inherit MyAbsClass()
    override this.Foo x = x + 2
    override this.Bar s = (base.Bar s) - 1
    override this.Baz x = x + 100
    member this.Z = z
    new () = MySubClass(0)

let c = new MySubClass(42)    
printfn "%d %d %d %d %d" c.Z (c.Foo 40) (c.Bar "two") (c.Qux 41) (c.Baz 42000)

推荐阅读
和谐啄木鸟
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有