在观看此 PDC 会议后,我想尝试学习一些 F#。 所以我认为最好的方法是用 F# 重写一些我已经用 C# 完成的东西,但我的大脑就是拒绝以功能模式思考。
我有这个抽象类,它有一些抽象方法和一些虚拟方法。 我也想重写一些虚拟方法。 这个类是用 C# 编写并编译的,我不打算用 F# 重写它。
所以我的问题是:
- 有没有人有一个关于如何实现抽象类、抽象方法和虚方法的简短示例
- 我可以重载构造函数吗?
- 如果我想将其编译为 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 格式的文件。
- Tomas Petricek 的《真实世界函数式编程》 这里有免费章节
一些其他可能感兴趣的资源:
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:
- does anyone have a short sample of how to implement an abstract class, abstract method and virtual method
- can I have overloaded constructors?
- 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:
发布评论
评论(1)
这是一些示例代码
Here's some sample code