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

发布于 2024-07-09 17:45:57 字数 2199 浏览 7 评论 0 原文

在观看 PDC 会议后,我想尝试学习一些 F#。 所以我认为最好的方法是用 F# 重写一些我已经用 C# 完成的东西,但我的大脑就是拒绝以功能模式思考。 我有这个抽象类,它有一些抽象方法和一些虚拟方法。 我也想重写一些虚拟方法。 这个类是用 C# 编写并编译的,我不打算用 F# 重写它。 所以我的问题是:

  1. 有没有人有一个关于如何实现抽象类、抽象方法和虚方法的简短示例
  2. 我可以重载构造函数吗?
  3. 如果我想将其编译为 dll 并使其可用于我的基于 C# 的程序,是否有任何限制?

任何帮助,将不胜感激。


更新: 我真的非常感谢布莱恩的回答,但我仍然不清楚,所以我想澄清一下。 假设这是我用 C# 编写并在 dll 中编译的抽象类。 如何在 F# 中实现它?

public abstract class MyAbstractClass
{
    public abstract Person GetFullName(string firstName, string lastName);

    public abstract bool TryParse(string fullName, out Person person);

    public virtual IEnumerable<Person> GetChildren(Person parent)
    {
        List<Person> kids = new List<Person>();
        foreach(Child person in GroupOfPeople)
        {
            if(person.Parent == parent)
               kids.Add(child as Person);
        }
        return kids;
    }

    public virtual IEnumerable<Child> GroupOfPeople { get; set; }
}

为任何正在寻找 F# 资源的人提供的一些文档: - 如果任何其他 F# 有兴趣获取我在 Don Syme(F# 创建者)博客上找到的一些文档 他的书《F# Expert》的免费章节。 您可以下载 doc 格式的文件。

一些其他可能感兴趣的资源:

After I've seen this PDC session I wanted to try to learn a bit of F#. So I thought the best way would be to rewrite in F# something I've already done in C# but my brain just refuses to think in functional mode.
I have this abstract class that has some abstract methods and some virtual methods. I would like to override some of virtual methods as well. This class is written in C# and compiled and I don't intend to rewrite it i F#.
So my question is:

  1. does anyone have a short sample of how to implement an abstract class, abstract method and virtual method
  2. can I have overloaded constructors?
  3. are there any constraints if I want to compile it in a dll and make it available to my C# based programs.

Any help would be appreciated.


Update:
I really really appreciated Brian's answer but it is still not clear to me so I want to clarify. Let's pretend this is my abstract class written in C# and compiled in dll. How do I implement it in F#?

public abstract class MyAbstractClass
{
    public abstract Person GetFullName(string firstName, string lastName);

    public abstract bool TryParse(string fullName, out Person person);

    public virtual IEnumerable<Person> GetChildren(Person parent)
    {
        List<Person> kids = new List<Person>();
        foreach(Child person in GroupOfPeople)
        {
            if(person.Parent == parent)
               kids.Add(child as Person);
        }
        return kids;
    }

    public virtual IEnumerable<Child> GroupOfPeople { get; set; }
}

Some documentation for whomever is looking for some F# resources:
- if any other F# is interested to get some documentation I found on Don Syme's (creator of F#) blog free chapters of his book F# Expert. You can download those in doc format.

Some other resources that might of interest:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

标点 2024-07-16 17:45:57

这是一些示例代码

type IBaz =
    abstract member Baz : int -> int

[<AbstractClass>]
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)

Here's some sample code

type IBaz =
    abstract member Baz : int -> int

[<AbstractClass>]
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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文