定义类和接口

728 篇文章 1 订阅
86 篇文章 0 订阅

定义类和接口

 

在 F# 中,有两种方式为函数和类的成员定义参数:“curried”风格,成员可以散(partially)应用,“元组(tuple)”风格,所有成员都必须一次给定。定义类时,如使用元组风格,C# 客户端可以更容易使用这样的类。

看下面的例子,在 F# 中定义一个类,其中有一个curried 风格定义的成员CurriedStyle,还有一个元组风格的TupleStyle。

 

namespace Strangelights

 

type DemoClass(z: int) =

  //method in the curried style

  memberthis.CurriedStyle x y = x + y + z

  //method in the tuple style

  memberthis.TupleStyle (x, y) = x + y + z

 

从 C# 中查看时,成员CurriedStyle 的签名是这样的:

 

public FastFunc<int, int>CurriedStyle(int x)

 

而TupleStyle 则是这样的签名:

 

public int TupleStyle(int x, int y);

 

因此,如果想要从 C# 中使用这两个成员,最终的代码会似这样的:

 

// !!! C# Source !!!

using System;

usingStrangelights;

usingMicrosoft.FSharp.Core;

 

class Program {

  static voidUseDemoClass() {

   DemoClass c = new DemoClass(3);

    FastFunc<int,int> ff = c.CurriedStyle(4);

    int result = ff.Invoke(5);

   Console.WriteLine("Curried Style Result{0}", result);

    result= c.TupleStyle(4, 5);

   Console.WriteLine("Tuple Style Result{0}", result);

  }

  static void Main(string[] args) {

   UseDemoClass();

  }

}

 

从这个示例可以很明显地看出,如果使用元组风格公开类的成员,库用户将会更满意的。

在接口和类中指定抽象成员,要稍微复杂一些,因为有几个更多的选择。下面以例子说明:

 

namespaceStrangelights

 

type IDemoInterface =

  // method in the curried style

  abstract CurriedStyle: int-> int -> int

  // method in the tupled style

  abstract TupleStyle: (int* int) -> int

  // method in the C# style

  abstract CSharpStyle: int* int -> int

  // method in the C# style with named arguments

  abstract CSharpNamedStyle: x : int * y : int -> int

 

注意,OneArgStyle 和MultiArgStyle 之间唯一的不同是后者不使用括号,在 F# 定义中的这一点不同,对从 C# 中的签名却有很大的影响。下面是前者的签名:

 

int OneArgStyle(Tuple<int, int>);

 

而后者的签名是这样的:

 

int MultiArgStyle(int, int);

 

对 C# 用户来说,后者显然更加友好一些。然而,我们也要付出一些,为每个参数添加名字。这并不改变 C# 用户使用的签名,当实现这个方法时,只改变在用Visual Studio 工具实现接口时看到的名字;此外,一些其他的.NET 语言会把参数名看得很重要。这听起来好像有点不同,但是,它使实现接口更加容易,因为,这种实现方法能够更好地表达参数真正的意思。

下面的例子演示C# 代码实现在前面的例子中定义的接口IDemoInterface,很明显,C# 用户更乐于接收包含由 MultiArgStyle 或 NamedArgStyle 指定的方法的接口。

 

// !!! C# Source !!!

using System;

usingStrangelights;

usingMicrosoft.FSharp.Core;

 

// shows how toimplement an interface

// that has been createdin F#

classDemoImplementation : IDemoInterface {

  // curried style implementation

  public FastFunc<int,int> CurriedStyle(intx) {

    // create a delegate to return

   Converter<int, int> d =

      delegate(int y) { return x + y; };

    // convert delegate to a FastFunc

    return FuncConvert.ToFastFunc(d);

  }

 

  // tuple style implementation

  public intTupleStyle(Tuple<int, int> t) {

    return t.Item1 + t.Item2;

  }

 

  // C# style implementation

  public intCSharpStyle(int x, inty) {

    return x + y;

  }

 

  // C# style implementation, named arguments

  // make no difference here

  public intCSharpNamedStyle(int x, int y) {

    return x + y;

  }

}

[

这一段描述中的名字与实际代码不符,但是,并不影响对文章的理解,故未订正。

]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值