有没有办法在辅助构造函数中设置自动属性?

时间:2015-09-09 19:39:08

标签: f#

我有这堂课:

type Sample() =
    member val test1 = "" with get,set
    member val test2 = "" with get,set

    // is something like the below constructor possible
    new Sample(result1, result2) =
        this.test1 <- "failed"
        this.test2 <- "passed"
        Sample()

我尝试了几种不同的方法,但我无法让它发挥作用。

1 个答案:

答案 0 :(得分:6)

这是你想要的吗?

type Sample(result1, result2) =
    member val Test1 = result1 with get,set
    member val Test2 = result2 with get,set
    new () = Sample("failed", "passed")

FSI:

> Sample();;
val it : Sample = FSI_0002+Sample {Test1 = "failed";
                                   Test2 = "passed";}
> Sample("foo", "bar");;
val it : Sample = FSI_0002+Sample {Test1 = "foo";
                                   Test2 = "bar";}